Hi:
I have a little question about programming the photoswitch, I use c++ (windriver) , we examined the line tacking template code that first made ,but we ended up creating our own .We made slight changes to the original code ,first the sensor that’s tracking the line is the middle one instead of the right one. Also we are trying to cover all the possible cases that the three sensors may have .
So my question is about case 0 (000),the case in which all the sensor’s digital inputs are 0,in other words the robot lost the track. According to our code in this case the robot stops. But that is so not practical ,so we were considering to program the robot to go backward in this case until it finds the track… I don’t know if this is the best solution ,so I am asking you to help me ….if you have any suggestions that will be great…
And here is the code
p.s we didn’t program the entire cases yet…
#include "WPILib.h"
#define GRAY_AREA 0.06 //The area that human hand may commit an error in .
#define MIN_SPEED 0.1 //the min speed that the wheels may get while turning.
#define INVERT -1 //invertieng a positive value to a neg value.
#define ANGLE2SPEED 0.06 // to convert Gyro angles to motor speed since angle is -30 to 30 and speed from -1 to 1
#define MAXSPEEDGYRO 0.7 // max speed in gyro mood before inverting correction from left to right
#define MAX 1 //MAX speed for the elec shooter.
#define FIXSPEED 0.3
#include "Gyro.h"
#include "Joystick.h"
#include "Compressor.h"
#include "DriverStation.h"
#include "PWM.h"
#include "Solenoid.h"
#include "Jaguar.h"
#include "Victor.h"
#include "Gyro.h"
/**
* This is a demo program showing the use of the RobotBase class.
* The SimpleRobot class is the base of a robot application that will automatically call your
* Autonomous and OperatorControl methods at the right time as controlled by the switches on
* the driver station or the field controls.
*/
class RobotDemo : public SimpleRobot {
//RobotDrive myRobot; // robot drive system
//Joystick stick; // only joystick
Jaguar FL;//FrontLeft motor.
Jaguar FR;//FrontRight motor.
Jaguar RL;//RearLeft motor.
Jaguar RR;//RearRight motor.
RobotDrive myRobot; // Robot drive system
Joystick Rightstick; // driving joystick
Joystick Leftstick; // shooter+hangr stick
//float rSpeed, lSpeed;
float spinspeed;
Compressor mycomp;
Solenoid gripper_o;
Solenoid gripper_c;
Solenoid Minibot_o;
Solenoid Minibot_c;
Victor spin;
DigitalInput *left; // digital inputs for line tracking sensors.
DigitalInput *middle;// digital inputs for line tracking sensors.
DigitalInput *right;// digital inputs for line tracking sensors.
DriverStation *ds; // driver station object for getting selections
Gyro myGyro;// This is the Gyro sensor(balancing the Robot).
public:
RobotDemo(void) :
FL(1), FR(2), RL(3), RR(4),
myRobot(&FL, &FR, &RL, &RR), // These must be initialized in the same order.
Rightstick(1),
Leftstick(2),
mycomp(4, 4),//( pressureSwitchChannel, compressorRelayChannel)
gripper_o(8, 1),//(solenoid out(slot,channel)
gripper_c(8, 2),//(solenoid out(slot,channel)
Minibot_o(8,3),//(solenoid out(slot,channel)
Minibot_c(8,4),//(solenoid out(slot,channel)
spin(5), // (Gaguar)
myGyro(1)
{
myRobot.SetExpiration(0.1);
mycomp.Start();
left = new DigitalInput(1);// Digital input on the sidecar
middle = new DigitalInput(2);//Digital input on the sidecar
right = new DigitalInput(3);//Digital input on the sidecar
}
/**
This is the Autonomous mode code,in this code we programmed a line tracker (photoSwitch),
we mounted 3 sensors on the robot with 1.5 inches apart.
It tracks the middle sensor(010) ,and corrects the robot's movement to follow the line
smoothly.
There are 8 possible cases,in each one the robot reacts ppropriately to resume following
the track.
*/
void Autonomous(void) {
//double defaultSteeringGain = 0.65; // default value for steering gain
int binaryValue;
//int previousValue = 0;
//double steeringGain;
float rAutSpeed=1,lAutSpeed=1;
myRobot.SetExpiration(15);
// set up timer for 8 second max driving time and use the timer to
// pick values from the power profile arrays
Timer *timer = new Timer();
timer->Start();
timer->Reset();
//int oldTimeInSeconds = -1;
double time;
//double lSpeed=rSpeed=1, turn;
float disc = 0; // Counter
FL.Set(0);
RL.Set(0);
//Right motors
FR.Set(0);
RR.Set(0);
while (time = timer->Get() < 15.0){
disc++;// increasing the counter (Add 1 )
/*
FL.Set(disc/10);
RL.Set(disc/10);
//Right motors
FR.Set(disc/10);
RR.Set(disc/10);
*/
int leftValue = left->Get() ? 1 : 0; // read the line tracking sensors
int middleValue = middle->Get() ? 1 : 0;
int rightValue = right->Get() ? 1 : 0;
binaryValue = leftValue * 4 + middleValue * 2 + rightValue;
switch (binaryValue){
//Covers the possible cases of the photoSwitches ,and how the
//Robot reacts in each case.
case 1:
rAutSpeed = 1 - (float) (timer->Get()/15 ) - FIXSPEED ; //Right Turn,there is higher speed at the right side.
lAutSpeed = 1 - (float) (timer->Get()/15);
break;
case 2:
rAutSpeed = 1 - (float) (timer->Get()/15);//Left Turn,there is higher speed at the right side.
lAutSpeed = 1 - (float) (timer->Get()/15);
break;
case 3:
case 4:
lAutSpeed = 1 - (float) (timer->Get()/15 ) - FIXSPEED ;
rAutSpeed = 1 - (float) (timer->Get()/15);
break;
case 5:
case 6:
case 7:
default:
rAutSpeed = 0;
lAutSpeed = 0;
}
FL.Set(lAutSpeed);
RL.Set(lAutSpeed);
//Right motors
FR.Set(rAutSpeed);
RR.Set(rAutSpeed);
Wait(0.05);
}
// stop driving when finished
FL.Set(0);
RL.Set(0);
//Right motors
FR.Set(0);
RR.Set(0);
}