Rover – Program for Controlling the Vehicle

Naturally, the first thing that we all want to do with our new vehicles is to turn them on and perform a test drive. The easiest way to do this is to use our mobile app LOFI Control. However you can also program the steering yourself in LOFI Blocks, and that’s what we are going to do in this lesson. We are going to use programming to assign keyboard keys to specific actions of our DC motors, thus making our vehicles move!

Preparations

  1. Construct any of the vehicles from CODEBOX Drive kit.
  2. Connect the robot to LOFI Blocks (online or mobile version).
  3. Make sure the DC motors in your vehicle are connected to M1 and M2 ports on LOFI Brain.

The Code

The easiest way to steer your robot

Before we head on to programming steering with the arrow keys, we would like to show you the most simple code possible that will move your vehicle. This “trick” uses the fact that the motor blocks in LOFI Blocks not only can take numeric values from 0 to 100 but also logical values true and false that will be interpreted as 0 and 100. Also, the blocks detecting keystrokes return values true/false. That is why our simplest code for steering the vehicle can look like this:

LOFI_Robot_programming_steering

As you can see, steering the vehicle this way can be quite effective. By pressing both left and right arrow key we make our vehicle go forwards, and pressing only one key will make the vehicle turn. We can’t go backward just yet. So let’s add more blocks in this fashion that will fix that issue:

LOFI_Robot_programming_steering

Did you notice how the robot jerks when going backward? Do you have an idea what can cause this behavior? Let’s look again at our script.

LOFI_Robot_programming_steering

During a single run of the REPEAT loop, the power of each motor is changed twice. M1 is first set for value LEFT, and then it’s set for value RIGHT. When only one key is pressed, the motors get different instructions very rapidly: 0, 100, 0, 100, 0, 100,… and so on. These quickly altering, contradictory instructions are the source of that jerking move. Analogically, the same happens to M2 motor.

So what’s the conclusion? During a single run of the loop REPEAT, the power of the motor should be set only once. Otherwise pressing other keys will send contradictory instructions to the robot. In order to avoid this, we will use conditional instructions.

Steering program with the use of conditional instructions

Please note that we set one motor to move and the other to stop when executing turning left and right. Thanks to this the movement of the robot is slower and easier to control. If you want to speed that up, set one motor forwards and the other backwards.

It may come to your attention that your robot doesn’t run in a perfectly straight line when moving forward – it can lean to the sides at times. Don’t worry, your robot isn’t broken. This is a normal behavior that is caused by some technical limitations.

LOFI Robot uses motors that run on direct current with fixed 40:1 transmission, no encoders. An encoder is a type of sensor built in the motor which returns signals to the microcontroller on how the motor turns. Encoders allow for precise information on the position of the motor as well as the distance made by the vehicle. Since our robots don’t use encoders, the microcontroller doesn’t get feedback from the motors. It’s only able to set the motor to a given state from 0 to 100.

That is why the microcontroller doesn’t “know” how the motors are performing, it doesn’t if the motors are running at the same speed. And different speed in DC motors can be caused by different friction in motors’ gears and slightly different original efficiency of the motors. Also, the small revolving wheel at the bottom of our vehicles can alter the robot’s path because they need a bit more time to set in the direction of the instructed movement.

The best solution to this is to experiment with the settings of the motors to fine-tune their performance. For example, M1 motor could be set to 60 and M2 to 55 to achieve straight forward movement.