"userpy"

New to forum and robotics class; I promise I searched this here!

Program works as expected, but brain displays this message in red:
Traceback (most recent…)
File “userpy”, line 44, in
File “userpy”, line 36, in no_object
[etc…]

My question is: what does “userpy” mean/reference?

Regards,
Randall

3 Likes

can you link your code in here?
I would guess that got written in or incorrectly defined in your code

Sure, thanks:
…and I have another question while you’re here!
when the program reaches the IF statement and meets the condition, the
“drivetrain.drive_for(REVERSE, 100, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)”

lines run one more time then everything stops. I expected the sys.exit() to stop everything immediately…

def no_object():
while (range_finder_g.found_object() == 0) and (brain.timer.time(SECONDS) < 5.0):
drivetrain.drive(FORWARD)
yes_object()

def yes_object():
if (brain.timer.time(SECONDS) > 5.0):
import sys
sys.exit()
drivetrain.drive_for(REVERSE, 100, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
no_object()

no_object()

sorry, I will send again with indentation…

use 3 backticks (`) to open or close a code block

like this:

//code
3 Likes

Like this:

```python
//code here
```

3 Likes

and that will keep the indentation I’m formatting in my post? Because I’m indenting while composing post, but post ignores it…

yes the code blocks will keep your code formatted

1 Like

You should also be able to see a message preview before you send it. And btw the python after the ``` formats the code colors to match python

thanks!

def no_object():   
    while (range_finder_g.found_object() == 0) and (brain.timer.time(SECONDS) < 5.0): 
        drivetrain.drive(FORWARD)
    yes_object()
def yes_object():
    if (brain.timer.time(SECONDS) > 5.0):
        import sys
        sys.exit()
    drivetrain.drive_for(REVERSE, 100, MM)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    no_object()
no_object()
1 Like

your issue appears to be a recursion. You can’t have 2 functions calling each other or else it will fill up and overflow the stack.

5 Likes

That and the fact that yes_object is undefined when first called in no_object

1 Like

Ok so that wont stop the program from running, the stack will just overflow and I’ll get error message?! (Is there a command to clear the stack dynamically?)

My other question is: why do the last two drivetrain commands (in yes_object) get executed, even though the IF statement condition was met previously?

I have class now but I can answer your questions after. In the meantime could you please explain generally what you’re trying to do with the robot, and then explain how you think your code should be executing it. That will help me and others to understand the best way to help you.

Thanks I understand!

Robot goes along its merry way forever (unless the brain timer has expired), until it detects an object in front of it

while (range_finder_g.found_object() == 0) and (brain.timer.time(SECONDS) < 5.0): 
        drivetrain.drive(FORWARD)

then when it detects something, it executes the yes_object block, which immediately checks to see if the reason it was called was because the timer ran out, in which case everything should end.

if (brain.timer.time(SECONDS) > 5.0):
        import sys
        sys.exit()

If timer hasn’t expired, then it backs up, turns, and the repeats all over again with the no_object block. So basically, it just roams around avoiding objects until the timer runs out. (I had set the timer to 5s just for testing)

From back in my Fortran/basic/pascal past dabblings, I’d been told that exiting out of a program this way was considered lame (for reasons I never fully understood). I imagine there’s a better way to do this, but was curious about the “userpy” message and also why the program doesnt seem to exit immediately with the sys.exit() command, but instead runs the last two drivetrain lines in the yes_object function before it stops. It doesn’t appear to call the no_obeject() function after that. The behavior is (with no obstacle): runs for five seconds, backs up, turns, quits.

1 Like

some answers
“userpy” is just the name given to the “user python” program, the VM does not know what the original name is.

sys.exit() does not stop the program, it throws an exception, that’s what is printing the red error popup and, because you are recursively calling functions, it would probably have many lines. Throwing an exception does not stop the motors so they will continue running with whatever the last command was.

The V5 brain does not run an OS in the conventional sense, so calls like sys.exit() will not work the same way as when running Python on a PC, I would avoid calls like that.

you should probably look at this topic, it will help when debugging Python programs.

8 Likes

Ok, I think I understand what you’re trying to do. Using functions (what you called blocks) in that particular way is not be the most straightforward approach. Here is an example of a much simpler way to do what you’re wanting:

drivetrain.drive(FORWARD)
while brain.timer.time(SECONDS) < 5.0:
    if range_finder_g.found_object() != 0:
        drivetrain.drive_for(REVERSE, 100, MM)
        drivetrain.turn_for(RIGHT, 90, DEGREES)
        drivetrain.drive(FORWARD)
drivetrain.stop()

If the timer runs out in the middle of backing up and turning, it will stop as soon as it’s finished. If you want it to abruptly stop regardless of what it’s doing exactly when the timer runs out, that is a bit more complex (but definitely doable)

2 Likes

hahaha oh my yes that is much more elegant; eeeks I need to scrape off the rust!
I knew I didn’t like having to check the timer twice, but I just got closeminded in my approach once I got the robot to actually move…

Thanks!

2 Likes

How would this work? Would the drivetrain.drive_for and drivetrain.turn_for methods just have to be non-blocking in a way so the program could continue to run the while loop as the robot moves?

Edit: Or is this a situation of using multithreading of some sort?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.