|
legov
Rookie
Joined: Fri Feb 18, 2011 12:25 am Posts: 1
|
 Writing a light sensor program
#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none) #pragma config(Sensor, S2, light2, sensorLightActive) #pragma config(Sensor, S3, light3, sensorLightActive) #pragma config(Sensor, S4, touch, sensorTouch) #pragma config(Motor, mtr_S1_C1_1, motorD, tmotorNormal, openLoop, encoder) #pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop, reversed, encoder) #pragma config(Motor, mtr_S1_C2_1, motorF, tmotorNormal, openLoop) #pragma config(Motor, mtr_S1_C2_2, motorG, tmotorNormal, openLoop) #pragma config(Servo, srvo_S1_C3_1, touchServo, tServoStandard) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#include "JoystickDriver.c" //Include file to "handle" the Bluetooth messages.
// Place code here to sinitialize servos to starting positions. // Sensors are automatically configured and setup by ROBOTC. They may need a brief time to stabilize. /* 2 is right, 3 is left. Light sensors Grey: #2: 25% #3: 17% White: #2: 41% #3: 34% */
task main() {
while(SensorValue(touch) == 0) // While the Touch Sensor is inactive (hasn't been pressed): { while(SensorValue(light2) < 30) { motor[motorD] = 10; motor[motorE] = 10; } while(SensorValue(light3) < 30) { motor[motorD] = 10; motor[motorE] = 10; } } } that is what we have but how would we write a line following program with 2 different light sensors straddling the line please help
|
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 Re: Writing a light sensor program
There are many ways to implement line following, from very simple to very sophisticated involving PID control. I will explain a simple way. To write a line following algorithm, consider this: - the robot is running forward at some speed - the robot may turn slightly left or slightly right according to the light sensors (i.e. if the line is detected on the left, you turn left, if it is detected on the right, you turn right). - assuming the white line will give a sensor value above some threshold number. - if the line is right at the center between the two ligth sensors, both light sensors will give you values above the threshold. - if only the right sensor gives you a value above threshold, the line is to your right. - if only the left sensor gives you a value above threshold, the line is to your left. With these considerations, the code would look something like this:
|