#๐Ÿ”’ Help breaking this while loop

64 messages ยท Page 1 of 1 (latest)

chilly ruinBOT
#

@shadow pilot

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

shadow pilot
#

why wont it let me upload my code : (

sterile estuary
#

If its too large to paste into chat, you can use the paste service:

#

!paste

chilly ruinBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

sterile estuary
#

Yeah, you can't upload a file

#

!code

chilly ruinBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

sterile estuary
#

Either paste it into chat and use the above to format it, or use the paste service if it is too long for that

shadow pilot
#

like that?

olive terrace
#

Yes

shadow pilot
#

okay nice

#

so basically, now that you guys can see my code, this is a lego mindstorms project for school with python. I want to log various parts of the robots movement actively as it moves around, this is just a simple test code. I have the issue where it keeps logging forever and doesnt move onto the next line

#

i want to know how i would break the while loop

sterile estuary
#

Can I assume its the while loop in motion that is the issue?

olive terrace
#

When do you want to break it?

#
def motion(left_motor, right_motor, gyro, straight_distance, x):
    while x == 1:
        data.log(watch.time(), left_motor.angle(), right_motor.angle(), gyro.angle(), straight_distance)
olive terrace
#

This is the problem?

shadow pilot
#

yes

shadow pilot
#

basically when the robot isnt moving

olive terrace
#

In this case I don't think you want the while loop in the function.

#

You'd probably have the while loop in your main body of code.

sterile estuary
#

It never gets to line 65, because line 64 takes forever as it gets stuck in a loop without a break and where x doesn't change.

shadow pilot
#

right

olive terrace
shadow pilot
#

๐Ÿค”

olive terrace
#

I don't really know much about ev3 though.

sterile estuary
#
motion(...)
wait(5)
motion(...)
wait(5)```
Which you could put into a loop, like a `for` loop that goes 500 times
shadow pilot
#

ev3.speaker.beep() # Signal that the gyro is resetting, do not touch after the beep
gyro.reset_angle(0) # Reset gyro
x=1
straight_distance = 0
straight_distance = distance(left_motor, right_motor, straight_distance)
drive.base(5)
while x == 1:
data.log(watch.time(), left_motor.angle(), right_motor.angle(), gyro.angle(), straight_distance)
wait(2000)
x=0
drive.base_stop
ev3.speaker.beep() # Signal that program is done

like this?

olive terrace
#

Instead of having a loop in the function itself.

shadow pilot
#

ohh i see

#

im not sure though, i wanted to make it a bit more clever

olive terrace
#

Sometimes the simplest ideas are best

shadow pilot
#

i am going to branch it out so i wanted an easier way to just stop the loop

#

because that would mean i would have to time how long every motion takes

#

the goal is to have this act as a part of an odometry system

sterile estuary
# shadow pilot i am going to branch it out so i wanted an easier way to just stop the loop

You're going to have to explain more about what other conditions would cause you to want to break the loop. Which variables having which values mean you want to stop? Other than reaching 2000 of wait time?

I was suggesting something like:

for _ in range(200):
    data.log(watch.time(), left_motor.angle(), right_motor.angle(), gyro.angle(), straight_distance)
    wait(10)
shadow pilot
#

okay so essentially, i want to have it so that while the robot is moving, it will log data, and then when it stops moving, it will stop logging data, and if it moves after that it will begin logging again

#

basically i want it to constantly check for movement

#

x is just a placeholder at the momenet

#

i will probably use a "if the motor speed is above 0, log data" type loop

#

but i think i will run into the same issue

sterile estuary
shadow pilot
#

right that makes sense

#

i am just curious as to how i would make it constantly check

#

because i assume if i would enable that function when the robot boots, it will see that there isnt movement and continue on

#

and not log anything even if the robot moves afterwards

sterile estuary
#

What do you call to actually move the bot?

#

Or is this just a monitor?

shadow pilot
#

currently i am using a timer

#

and a .run

sterile estuary
#

So in general, you can do, say drive.base(15) but during the execution of that ALSO do something else. Python generally only does one thing at a time unless you're doing threading or multiprocessing and most libraries aren't going to behave nicely for that. So if you want it to be constantly checking if it has moved, then you need to be constantly running an if statement that checks if it has moved. One way to do this would be to create your own move functions that move 1, log, move 1, log, and loop that until it hits its target amount of movement. Now you can just call your own custom move function when you want it to move

#

And if it is only moving when you've told it to move, then there isn't really a need to check if its moving or not, right? You can just have it log its movements occassionally throughout your custom movement function and don't log anything otherwise.

shadow pilot
#

true

#

i might get rid of continous logging

#

realistically i only need it once the movement is done

chilly ruinBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.