Controler and distance captor

Hi everybody!

I use the controler to make my robot move and while I control it, I want it to be able to move forward for 1 second by itself when it detects an object with the distance captor.

Yet, when it detects an object, instead of moving forward for one second (like said in my program), it does the last action I asked him to do with my controler. For example, if it was moving to the right when it detects an objets, it starts spinning to the right for a second.

Any idea of how to solve my problem?

Can you post the code?

Without the code, there are possibly an infinite number of things that could be the problem. Programming issues are a lot easier to solve when you have the code right in front of you.

2 Likes

Oups! I tought I did, sorry! Here it is:

Is there an English version? That would help.

I have an idea of what the problem might be, but I have to explain some background for it to make sense.

You have three loops running side by side, all three at the same time: your driver control loop, which is made automatically in the background when you configure the controller in the sidebar, your when started loop, and your when bumper pressed loop.

  • Your driver control loop is running, and when you push a button, it responds accordingly, and tells the motor to spin such-and-such direction.
  • Your when started loop is running: it checks whether the distance is less than 50, and if it is, tells (something I can’t read) to (do something I can’t read). Then it tells its loop to stop executing for one second. Then it comes around again and checks if bumper is pressing, and if it is, plays a sound, sets a touch LED to a color, tells its loop to wait three seconds, and changes the color.
  • Your when bumper pressed loop is running, and it checks if bumper is pressing, and if it is, adds one to NbrBumper, moves the cursor, and prints NbrBumber on the brain, and tells its loop to stop for 0.5 seconds

It would help if you could tell me what the code is in English. Try going into the code sidebar on the right and copying and pasting what you see. I think that would be in English. Otherwise, go into the globe icon in the top left corner and change it to English and take a screenshot.

2 Likes

Or you could take the screenshot and translate it to English via Google Translate:

5 Likes

That Google Translate code is… interesting.
From what I see it says,

(left side)
When Started
Set Motor Speed to 100%
forever
if bumper8 is pressed, then
play the sound (alarm)
wait 3 seconds
set the colour of the TouchLED2 to None
if the object distance of Distance7 in mm is less than 50 then
drive backwards
wait for 1 second
stop driving

(right side)
when Bumper8 is pressed
forever
if Bumper8 is pressed then
set NbrBumper to 1
Set cursor on line 1 column 1 on Brain
print NbrBumper on Brain
wait 0.5 seconds

(I am Canadian, so if I typed some things that are incorrect the the USA, please forgive me, it should be understandable.)

Thank you, that helps.

@Myriam, it’s generally easier to read and understand code if you leave it all in one loop. I would recommend getting rid of your when bumper pressed loop. Do you see that the if block inside the forever loop under the when bumper pressed will check whether bumper is pressing forever? I would recommend moving all the code inside that if into the if bumper is pressing under the when started block.

As for the original issue, your auto-generated controller loop is still running, because the wait block only stops the code underneath it, and the controller code is separate. Do you see a block that says something like disable controller? I think if you put in your if distance < something like

disable controller
set drive velocity to 100%
drive forward
wait 1 second
enable controller

The disable controller block stops the auto-generated controller code.

You said,

What looks to you like “spin right” is actually “right wheel spin forward slower than left wheel”. So when you say, “Spin both wheels forward (drive forward),” it says, “Both wheels are spinning forward.” That’s why you need to set the drivetrain velocity to 100%.

The next problem is that the controller loop is still running. So the controller loop checks where your joysticks are and sees that they are both at 0, and sets the drivetrain velocity to 0. So you need to disable controller to stop the controller loop from checking the joysticks and setting the drivetrain velocity.

3 Likes

I already tried to disable and enable the controller but didn’t think about changing the drivetrain velocity. It works perfectly now! Thanks to all of you who helped me!

2 Likes

It would be good to mark that last post that got you there as the solution, then others can find that more easily if they’re having a similar problem.

Glad you got it fixed!

2 Likes