Controlling Vehicles with the Keyboard

How to make a vehicle steering program using LOFI Blocks.

Naturally, the first thing that we all want to do with our new robotic 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.

Programming

Let’s begin with controlling the motors. Place your vehicle on some small platform, so that the wheels can move freely, but the vehicle remains in place – you can use the nuts and bolts box for that purpose.

To make the motors move, we will use the motor movement block. Place that block in the loop, and try putting in various parameters to see how the motors will respond.

LOFI_Robot_programming_steering

  • We’ve got two motors connected to M1 and M2 ports.
  • Each motor can move forwards or backwards.
  • We can define the motors’ power in scale from 0 to 100, where 0 means motors off, and 100 is maximum power.

Notice. The DC motors that we’re using, like any motors, have their own technical limitations. Ours have fixed transmission which means that they provide low power in “lower gears”. In practice, if you assign too little power (under 40) to the motors, your vehicle won’t have enough energy to move. It’s like trying to move a bicycle on the lowest gear – it takes a lot of force.

Let’s perform an experiment. Turn on the motors on 100 power, and then gradually decrease the power all the way to 0 (you can use the potentiometer). In this case, the motor will slow down smoothly. However, if you do the opposite thing, start from zero and gradually go up to 100, you will see that the motors won’t start moving until around 50% power.

Since we don’t have blocks that enable directly steering the robot, like “go forward” or “go left”, we have to program the movement “from scratch” by programming the motors’ movement. In practice:

  • Go forwards is both motors M1 and M2 set to forward direction.
  • Go backwards is both motors M1 and M2 set to backward direction.
  • Turn left is M1 set to forward and M2 set to backward direction.
  • Turn right is M1 set to backward and M2 set to forward direction.

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

Notice. Place the robot back on the platform, so that the wheels can turn without moving the robot.

In the control section we can use two types of conditional instructions:

  • Single condition – IF.
  • Double condition – IF… ELSE…

We want our program to start the motors when a key is pressed, and stop the motors, when a key is not pressed. Let’s try a double condition then:

  • If the UP key is pressed (logical value TRUE), set the motors M1 and M2 to 100 (go forwards)
  • Otherwise (logical value FALSE), set the motors M1 and M2 to 0.

LOFI_Robot_programming_steering

If we run our program now and press and hold UP arrow key, our robot will move forward, and when we let the key go, the robot will stop. It seems that the program works well enough, so let’s add moving backwards after pressing the DOWN key.

LOFI_Robot_programming_steering

Go ahead and test that program. Is everything working ok? Maybe our robot will sometimes work well, but we can notice the same jerks as before. That’s because this program has a similar problem – we’re allowing to set the motors to different values at the same time. When the UP key is pressed, the robot moves forward, but then it has to do what the program instructs him when the key DOWN is not pressed, that is stop the motors.

We can solve this problem by switching double conditions to single conditions.

LOFI_Robot_programming_steering

Better, but the vehicle doesn’t stop when we let go the keys. Fortunately, our motor block has a “none” option which returns value TRUE when no key is pressed – and that’s what we need. Let’s add a third condition – when no key is pressed, set M1 and M2 on 0.

LOFI_Robot_programming_steering

Looks like we’re on the right path. The robot doesn’t have the jerks anymore, because it doesn’t get contradictory instructions and performs only one action at a time (unless we press two keys simultaneously). Let’s go ahead and add missing directions left and right.

LOFI_Robot_programming_steering

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.

Congratulations! You have completed your steering program!

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 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.