const int thresh = 50; // light sensor thresh
const int power = 40; // motor power
const int factor = 2; // multiplicative sweep increase
const int overshoot = 20; // turn additional after line found
const int initialTurn = 8; // first sweep size
int spin_left(int deg) {
nSyncedTurnRatio = -100;
nMotorEncoderTarget[motorA] = deg;
motor[motorA] = power;
}
int spin_right(int deg) {
nSyncedTurnRatio = -100;
nMotorEncoderTarget[motorA] = -deg;
motor[motorA] = -power;
}
int straight() {
nSyncedTurnRatio = +100;
motor[motorA] = power;
}
void stop() {
motor[motorA] = 0;
}
bool online() {
return SensorValue(lightSensor) < thresh;
}
void resetMotorEncoder() {
nMotorEncoder[motorA] = 0;
}
bool stateIdle() {
return nMotorRunState[motorA] == runStateIdle;
}
task main()
{
bFloatDuringInactiveMotorPWM = false;
nSyncedMotors = synchAB;
int lastDir = 0;
while (true) {
while ( online() ) { straight(); }
stop();
resetMotorEncoder();
int turn = initialTurn;
for ( int i = lastDir; !online(); i++ ) {
lastDir = i%2;
for ( int j = turn; j && !online() ; j-- ) {
if ( lastDir ) { spin_left(1); }
else { spin_right(1); }
}
turn *= factor;
}
if ( lastDir ) { spin_left(overshoot); }
else { spin_right(overshoot); }
while ( !stateIdle() ) { wait1Msec(1); }
}
}
Wednesday, January 16, 2008
NXT Line Follower (via RobotC)
This time via RobotC (http://www.robotc.net)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment