Skip to main content

Introduction to Robotics

Section 6.4 Lab: Precise Turning

In this lab we’ll learn how to rotate the robot for a prescribed angle, for example in order to make a right angle turn.

Subsection 6.4.1 Basic Rotation

You already know the basic parts of carrying out a rotation probably: Turn the two wheels both clockwise for a left turn or both counter-clockwise for a right turn.

Practice 6.4.1.

Implement a basic rotation for a specified amount of time but including a ramp-up and ramp-down period. I.e. given an amount of milliseconds, the bot should ramp up to its maximum turn speed, then keep turning at max speed for that number of milliseconds, then ramp-down to stop.
Using a bit of trial and error, try to make the bot do a full 360 turn this way, by choosing the correct number of milliseconds.
This is the first objective for this lab.

Subsection 6.4.2 Precise Rotations

In order to do more precise rotations we will need to do a work similar to what we did for moving straight:
  • Have the bot rotate for given amounts of time, say from 0 and increasing by 100 milliseconds
  • Measure the corresponding angles
  • Now for a given target angle find the two closest values and use scale to find the correct number of milliseconds
This will work great, except for one key problem: How do we precisely measure angles? Eyeballing it will not cut it here. And measuring angles is more complicated than measuring on a straight line. So we will use a bit of math to help us, with something called the law of cosines: If we have a triangle with sides \(a\text{,}\) \(b\) and \(c\text{,}\) and the angle opposite side \(c\) is \(C\) then we have the equation:
\begin{equation*} c^2 = a^2 + b^2 - 2ab\cos(C) \end{equation*}
And we can solve this equation for \(C\text{:}\)
\begin{equation*} \cos(C) = \frac{c^2 - a^2 - b^2}{2ab} \end{equation*}
The idea is to use this to measure the angles, as follows:
  • Place your bot at an initial location and direction.
  • Have it move straight for a fixed distance, say 10 inches. We already know how to do that from last time. This is basically the value of \(a\) in the math above.
  • Have it turn for the specific amount of time. Essentially forming the angle C.
  • Then have it move for another specific distance \(b\text{,}\) say also 10 inches to make the math easier.
  • Now compute the distance \(c\) from the bot’s original location to its current location.
  • Use the formula above to compute \(\cos(C)\text{.}\)
  • Use the "inverse cosine" function, usually denoted by acos in most systems, to get the corresponding angle value.
  • The angle value is in "radians". To turn it to degrees you need to divide by 6.28 (i.e. \(2\pi\)) then multiply by 360. Or you can use scale with the ranges \((0, 2\pi)\) to \((0, 360)\text{.}\)

Practice 6.4.2.

Carry out the above work for time intervals increasing by 100 milliseconds each time, until you have reached a full circle (you probably have an estimate of how long that takes already). You may use 200 millisecond increments instead if you would have had way too many measurements to do (plan for more than 10 and less than 20 measurements).
Record your work in a spreadsheet, with columns for the amount of time run, the length of \(c\text{,}\) the corresponding computation of \(\cos(C)\text{,}\) and the value for C. You will need to use formulas to make that happen, to have the spreadsheet compute things for you. Let me know if you need assistance with that.
This spreadsheet is the second objective for this lab.

Practice 6.4.3.

Using lists of values from this table together with the scale function build a rotate function that is given an angle in degrees and it is supposed to rotate the robot exactly to this angle.
Then use this function to have the robot move in a perfect square of side 1 foot. The robot should end where it started.
Also have your robot move in a perfect equilateral triangle of side 1 foot.
Carrying out this last practice is the third and last objective for this lab.