The RoboMentors will introduce you to an advanced technique to go faster and then slower, still using the gyro to maintain a straight trajectory
What you will learn:
- How to control the speed of the robot
- How to plan the speed (faster, slower) along the way
- How to think about gyro errors and reset during longer missions
Keep the Gyro Sensor and EV3 Brick steady when connecting the cable and during start-up of the EV3 Brick.
What you need to review:
- Lesson 4 Going straight with Gyro
- Lesson 6 Variables and MyBlocks
When going a long way, going slow makes us waste time, while starting and stopping abruptly at high speed jolts the robot out of its trajectory.
Gently accelerating and decelerating would therefore help. We can for instance plan to accelerate for half of the way and then decelerate for the other half.
We are going to put to use the variables and math formulas, to compute the correct speed and feed it into the drive block. This way, the drive block inside the gyro following loop will have both the direction and the speed controlled by the math we are going to program in this myblock.
So we have three parameters: distance, initial speed and maximum speed. The robot will make sure it is stopped, and read its current gyro angle, and then do its best to keep it when it travels (using a loop, as we have already seen in the lesson of the gyro following).
Since we can control the speed of the robot by simply giving a number between 1 and 100 to the drive-steer power input, we can use the loop to also compute the fraction of the first half the robot has already covered, to know what its speed should be: the initial speed, plus the computed fraction times max speed minus initial speed. This way we start at the minimum speed and we reach maximum speed at the middle of the trajectory. Observe that this computed fraction is a number which varies from zero, at the start, to one at the middle.
Once we are past the middle, we do exactly the opposite: we compute the fraction of the remaining distance out of the second half. This will be a number varying from one to zero. We can use a very similar formula, which will now gently decrease the speed, from max to the initial one, when we will terminate the loop and stop.
Of course, inside the loop, as we said, also the gyro will be read and steering corrections applied as needed, independently of the speed formulas.
One last thing: our formula for the speed is in fact a group of two formulas. How can we combine them ? What we actually need is a way to say if the robot is in the first half of the trajectory or in the second one. It turns out there is a math operation which just checks if a value is in an interval, so its answer will be a one, if true, and a zero if false. We know that one times some number is that number, zero times some number is zero regardless of the number, and one minus a zero or a one is its “opposite”: one minus zero is one, and one minus one is zero. We can therefore use these “switches” to combine the formulas. We will have one formula or the other formula give the actual speed by multiplying them with ones and/or zeros, appropriately, and only one will “survive”.
So let d be the speed difference (d = v_max – v_ini) and let a be the number which is one if the robot is in the first half and zero otherwise (meaning the robot is in the other half). The speed would then be:
v_ini + (a*dist/half + 1-a)*((1-a)*(tot-dist)/half + a)*d,
where dist is the distance covered so far from start, and tot is the total distance.
- Stop Drive motors B+C
- Read current position motor B and store as iniDist
- Compute Distance/2 and store as halfDist
- Compute maxSpeed – iniSpeed and store as deltaSpeed
- Read current angle to keep and store
- Subtract current position motor B from iniDist, to compute how far we have traveled
- Compute fraction of that traveled distance out of half distance
- Test if distance traveled so far is less than half, to decide if we still accelerate
- Do the same as if we were in the second half
- Pick the right one using the decision (green wire in diagram below)
- Feed into drive-steer block speed input
- Compute direction correction from gyro as before and feed into drive-steer drive input
- Decide if we stay in the loop (distance traveled still smaller than stopDist)
A last few words about the gyro errors.
During runs, the gyro readings can become less and less precise. So even if the robot starts perfectly aligned with one of the walls, and thus its gyro says zero, after some time, if we place the robot again perfectly aligned with one of the walls the gyro will not necessarily give the same zero. If we do this by hand, between missions, it is all good. However, if we want to do it during missions, autonomously, then we need to give the robot some certainty about its position, in a different way, and then ask it to reset the gyro.
One way to do that is to have the robot gently drive for instance with its back into the wall, assuming the robot’s back is fairly flat. After a second or two of gentle driving, assuming the robot manages to arrive flat against the wall, the program can stop all motors, and request a gyro reset. This way, from that point of the program onwards, the gyro would again have fairly precise values, much closer to the real orientation of the robot.
A small error on the gyro is easily amplified if we rely on a precise orientation to travel more than a few inches or dozens of centimeters on the mat, so this is important.
Finally, it might be better to always use only one ball at the back of the robot, to only have it have three points of support: the two wheels and the ball. This way it is self-leveling and it will not slide in places where the competition table is not completely flat, so turns will happen with more certainty.