Thursday, November 21, 2019

Autonomous Car - mBlock Coding Explanation

I have been coding my mbot to follow a black line and to stop if something is put in front of it.
Here is all of my code.




Here is a video of my code working (please excuse it's sideways viewpoint)




Here is an explanation  of how my code works.




At the top of my code there is the start up block that lets my mbot know there is a line of code coming.
I then have a wait control block that is connected to a sensor block that is the button on the mbot.
I have added this so that the mbot will not start the coded actions as soon as it is turned on. I can wait till im ready and press the button for it to start.





The next thing is an operator block and a sensor block. This is saying to check if there is anything less than 10cm away from the front ultrasonic sensor.


I then used an "if, then, else" control block.
I used this because it gives the chance to put two different options of actions for the mbot to do depending on a condition.










The ultra sonic block is placed in the if.
This is saying that if something is less than 10 cm away from the ultrasonic sensor, the mbot does the actions in the "then" section.

These actions are:-

  • The mbot will stop, so it doesn't run the thing in front of it over
  • It will show red light to show it is stopping
  • It will make a noise as a warning


















The movement around the black line is put into the "else" section of the main control block. This is because it is the other thing the mbot has to do if there is nothing in front of it.

These sections are all put into "if, then" control blocks. This is because there needs to be a condition happening for the action to happen. If that condition is not happening then it will move onto the next code block.



There will be 4 of these and they will be based around this binary information.

Left Right21
Black Black000
Black White011
White Black102
White White113

This works like this ...


The code for this section, with all 4 sections, looks like this, where all of these 4 possibilities are in their own "if, then" control blocks.



While it is following the line, a green light is showing so this is the first thing inside the else section.



All of these "if, then, else" and "if, then" blocks are wrapped by one forever around the outside. This is because I want the mbot to continue to check for these conditions all the time. If the forever was not there it would do it all once then stop.

No comments:

Post a Comment