Programing the robot to use the light sensor to avoid light

From ROBOTC API Guide
Jump to: navigation, search
ArduinoArduino Tutorials and Guided ProjectsLego + Arduino, Mobile Robotics Platform → Programing the robot to use the light sensor to avoid light

Contents

A number of examples of the light avoiding program


Say you wanted your robot to avoid light - perhaps your robot's photosensory is too photosensitive, or you want to hunt vampires. We can do this by a small modification of the previous program, yet end up with drastically different actions. This is one of the powers of programming - once you have a solid code base, you can easily manipulate it to get a variety of interesting results.

All we need to do is change the code so that shadeDiff is subtracted from the leftServo, and added to the rightServo, so that the robot turns the opposite direction (thus away from the light) in any given situation.

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldDFRobotMotor)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, anlg0,  leftLight,      sensorReflection)
#pragma config(Sensor, anlg1,  rightLight,     sensorReflection)
#pragma config(Motor,  motor_5,         leftServo,     tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl5, dgtl4)
#pragma config(Motor,  motor_6,         rightServo,    tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl6, dgtl7)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
 while(true)
  {
    //Find the shade differential
    int shadeDiff = ((SensorValue[rightLight] * 100) / (SensorValue[leftLight] + SensorValue[rightLight]) - (100 / 2)) * 2; 
 
    //Use the shade differential to modify the motor speeds
    motor[leftServo] = 100 - shadeDiff;
    motor[rightServo] = 100 + shadeDiff;
 
    //If the loop cycles too quickly, the debugger can return incorrect values. (this is not a bug)
    //adding a small delay solves this problem
    wait1Msec(10);
  }
 
}

Problems with the Program

  • The light sensors tend to see a light to the front the same way they see a light to the back, so the robot may actually "follow" a forward light if it is directly in front of the robot.
  • The robot does a much better job "homing in" on the light than it does trying to evade it - it's much harder to control the avoid program than the follow program

Benefits of the Alternate Setup

For the same reasons that the alternate setup benefited the robot during the follow light program, the setup also benefits the robot in the avoid light mode. It still suffers the same drawbacks mentioned previously along with the ones that are associated with the alt setup.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox