0

Line Robot - Basic lessons - IMU

Our robot sports a very nice IMU (Inertial Measurement Unit), consisting of 3 sensors:
  1. a compass, showing where north is,
  2. a gyroscope, sensing rate of rotational change,
  3. an accelerometer, measuring acceleration.
We will not be using the gyroscope or accelerometer directly because these values are not especially useful, but the calculated are. They show us where the north is and how much the robot is inclined around longitudinal and transversal axis. For example, the last value can be used to check if the robot is on a slope. Here is a simple program that prints north pole's direction (a compass):
void RobotLine::loop() {
	print("%i\n\r", (int)heading());
}
We already know the print() function. A new part is:
(int)heading()
First about the "heading()". This is a function that returns north's direction, in degrees - a compass. However, its result is a decimal number. Can "print()" display decimal numbers? No, it cannot. Therefore, there has to be a bridge that will take the decimal number the compass produce and convert it into an integer, that "print()" knows how to cope with. The obvious candidate is the prefix "(int)" and it is the right solution. There are some more complicated concepts here, but it is the best for You to remember that "(int)" converts a decimal number into an integer. In fact it erases the decimals. Therefore:
  • (int)3.14 is 3
  • (int)4.999 is 4
  • (int)-1.5 is -1

Task: rotate and stop.

Start rotating the robot around its brunt and stop when it points nearly to 90° inclination from the north.