0

Soccer Robot C - Basic lessons - Reflectance sensors

MRMS reflectance sensors 9x, CAN, analog, I2C (mrm-ref-can) is a quite versatile sensor. Here, we will learn only the simplest form of measuring: digital values, meaning it either detects (for example) a white stripe or not. Prerequisite for this functionality is calibration. The sensor must be calibrated.
  1. Run the program, position the robot near a white stripe on a dark surface (like Robocup Soccer Junior playfield), and be ready to move it by hand.
  2. Enter command "cal".
  3. Immediately start moving the robot above the black stripe and over the white stripe, so that all the transistors of all the 4 sensors can detect the stripe and dark background. During the calibration, sensors' LEDs will be blinking rapidly. The end of calibration is indicated by a long LED-on. After that, normal 1-sec. blinging will continue.
  4. Stop moving the robot and issue a new command: "pri" (calibration print).
The last command will list 4 groups (1 for each sensor) of 2 lines of numbers. The first line should be around 300 and less for all the active transistors and the second one around 700 and more. The difference between the 2 numbers in the same column must be at least 400. If so, the calibration was a success. The values will be stored in the sensors and will be retained even when the power is switched off. Only when the surface changes significantly, or the light does that, a new calibration might be desirable.
The calibrated sensor can be read easily. To do it using the terminal, enter "dgr" (digital read). Watch 1s representing dark background below and 0s white stripe. To read the sensor in Your program, use command
line(transistorNumber, deviceNumber))
transistorNumber is a number between 0 and 9, for MRMS reflectance sensors 9x, CAN, analog, I2C (mrm-ref-can). deviceNumber is sensor's ordinal number: 0 is front, 1 is right, 2 rear, and 3 left reflectance sensor. The function returns "true" if the surface below is white (not black or dark green). Having learned all that, we can make a program that detects a white surface (line) with right sensor:
void RobotSoccer::loop() {
	if (line(0, 1))
		print("Line\n\r");
}
How can we detect a dark surface (like dark green)? line(0, 1) is a logical expression, because it returns either "true" or "false" (like "2<3"). We also learned how logical not operator looks like: "!". Therefore,
!line(0, 1) 
This was an example how to use the sensor in digital mode. It can also be used in analog mode in which every transistor returns a number in range between 0 (completely dark) and 1023 (very bright). The numbers in this range were reported by "pri" command. You can also use it program, by calling function "brightness()":
void RobotLine::loop() {
	print("%i\n\r", brightness(0, 1));
	delayMs(100);
}
"brightness(0)" is for reading transistor 0 (of sensor 1), but You can use 1, 2, etc., depending on how much transistors Your sensor features. Do not pay attention to the parts of this program You do not understand. You will learn that functionality later.

While reading either digital or analog values occurs momentary (from cache), switching between the 2 modes causes some cost in lost time, typically between 2 ms (to digital) and 15 ms (to analog). Therefore, it is advisable not to change the mode very often.

Task: print reflectance values

Your task is to print "White" when there is a white surface below transistor 8 of the rear reflectance sensor (C++ starts counting from 0), so this is sensor 7 in C++ notation.