📄 car.c
字号:
MotorLeftDir(0);
}
//
// If the delay is about to reach 0, start the left motor.
//
if(g_ucDelay == 1)
{
MotorLeftRun();
}
//
// Done with this mode.
//
break;
}
//
// The car is in diagnostic mode 5.
//
case MODE_DIAG5:
{
//
// If the delay has reached 50ms, reverse the direction of the
// right motor.
//
if(g_ucDelay == ((50 * SYSTICK_CLOCK) / 1000))
{
MotorRightDir(0);
}
//
// If the delay is about to reach 0, start the right motor.
//
if(g_ucDelay == 1)
{
MotorRightRun();
}
//
// Done with this mode.
//
break;
}
//
// The car is in diagnostic mode 6.
//
case MODE_DIAG6:
{
//
// If the delay has reached 50ms, set the direction of the left
// motor to backward.
//
if(g_ucDelay == ((50 * SYSTICK_CLOCK) / 1000))
{
MotorLeftDir(0);
}
//
// If the delay is about to reach 0, start the left motor.
//
if(g_ucDelay == 1)
{
MotorLeftRun();
}
//
// Set the left and right motor speeds based on the value of sensor
// zero.
//
MotorLeftSpeed((g_pusSamples[0] / 15) + 32);
MotorRightSpeed((g_pusSamples[0] / 15) + 32);
//
// Done with this mode.
//
break;
}
//
// The car is in diagnostic mode 7.
//
case MODE_DIAG7:
{
//
// Set the left and right motor speeds based on the value of sensor
// one.
//
MotorLeftSpeed((g_pusSamples[1] / 15) + 32);
MotorRightSpeed((g_pusSamples[1] / 15) + 32);
//
// Done with this mode.
//
break;
}
//
// The car is in diagnostic mode 8.
//
case MODE_DIAG8:
{
//
// Set the left and right motor speeds based on the value of sensor
// two.
//
MotorLeftSpeed((g_pusSamples[2] / 15) + 32);
MotorRightSpeed((g_pusSamples[2] / 15) + 32);
//
// Done with this mode.
//
break;
}
//
// Handle all other modes.
//
default:
{
//
// There is nothing to be done for this mode.
//
break;
}
}
//
// Handle blinking the lights if in diagnostic mode to indicate the current
// mode.
//
if((g_eMode >= MODE_DIAG2) && (g_eMode <= MODE_DIAG8))
{
//
// See if the current time is divisible by a quarter second.
//
if(!(g_usCount % (SYSTICK_CLOCK / 2)))
{
//
// If another blink is required to indicate the current mode, then
// turn on the lights now.
//
if((g_usCount / (SYSTICK_CLOCK / 2)) <= (g_eMode - MODE_DIAG1))
{
//
// Turn on the left head light when driving the left motor
// forward, the left tail light when driving the left motor
// backward, the right head light when driving the right motor
// forward, the right tail light when driving the right motor
// backward, and all lights in other modes.
//
if(g_eMode == MODE_DIAG2)
{
LightsOff();
TailLightsOff();
LightLeftOn();
}
else if(g_eMode == MODE_DIAG3)
{
LightLeftOff();
TailLightLeftOn();
}
else if(g_eMode == MODE_DIAG4)
{
TailLightLeftOff();
LightRightOn();
}
else if(g_eMode == MODE_DIAG5)
{
LightRightOff();
TailLightRightOn();
}
else
{
LightsOn();
TailLightsOn();
}
}
}
//
// See if the current time is divisible by an eighth second.
//
else if(!(g_usCount % (SYSTICK_CLOCK / 4)))
{
//
// Turn off the lights now.
//
LightsOff();
TailLightsOff();
}
//
// Increment the count, wrapping back to zero two seconds after the
// last flash of the lights.
//
g_usCount++;
if(g_usCount == (((g_eMode - MODE_DIAG1) * (SYSTICK_CLOCK / 2)) +
(SYSTICK_CLOCK * 2)))
{
g_usCount = 0;
}
}
//
// Decrement the delay if it is not zero.
//
if(g_ucDelay)
{
g_ucDelay--;
}
}
//*****************************************************************************
//
//! Handles the press of the push button.
//!
//! Handle the initial push button press, which typically means that the car
//! should be stopped if running. When in diagnostic mode, this will advance
//! the car through the various steps of diagnostic mode.
//!
//! \return None.
//
//*****************************************************************************
void
CarStop(void)
{
//
// Determine what to do based on the current mode.
//
switch(g_eMode)
{
//
// See if the car is running.
//
case MODE_IN_OPEN:
case MODE_APPROACHING:
case MODE_FOLLOWING:
case MODE_TURN_LEFT:
case MODE_TURN_RIGHT:
case MODE_TURNING:
case MODE_FORWARD:
case MODE_RANDOM_LEFT:
case MODE_RANDOM_RIGHT:
case MODE_RANDOM_TURN:
case MODE_RANDOM_FORWARD:
{
//
// Indicate that the car is now stopping. This is so that the
// subsequent button relese does not cause the car to start running
// again.
//
g_eMode = MODE_STOPPING;
//
// Stop the motors.
//
MotorLeftStop();
MotorRightStop();
//
// Clear the internal state so the telemetry data looks clean.
//
g_usLeftSensor = INFINITY;
g_sLeftDelta = 0;
g_usRightSensor = INFINITY;
g_sRightDelta = 0;
g_usFrontSensor = INFINITY;
g_sFrontDelta = 0;
g_usMotorLeft = SPEED(0);
g_usMotorRight = SPEED(0);
//
// Turn off the lights.
//
LightsOff();
TailLightsOff();
//
// Done handling this mode.
//
break;
}
//
// In diagnostic step 1.
//
case MODE_DIAG1:
{
//
// Advance to diagnostic step 2.
//
g_eMode = MODE_DIAG2;
//
// Set the light count to zero.
//
g_usCount = 0;
//
// Make the left motor run half speed forward.
//
MotorLeftSpeed(50);
MotorLeftDir(1);
MotorLeftRun();
//
// Done handling this mode.
//
break;
}
//
// In diagnostic step 2.
//
case MODE_DIAG2:
{
//
// Advance to diagnostic step 3.
//
g_eMode = MODE_DIAG3;
//
// Set the light count to zero.
//
g_usCount = 0;
//
// Stop the left motor.
//
MotorLeftStop();
//
// Reverse the direction of the motor over the next 100ms.
//
g_ucDelay = (100 * SYSTICK_CLOCK) / 1000;
//
// Done handling this mode.
//
break;
}
//
// In diagnostic step 3.
//
case MODE_DIAG3:
{
//
// Advance to diagnostic step 4.
//
g_eMode = MODE_DIAG4;
//
// Set the light count to zero.
//
g_usCount = 0;
//
// Stop the left motor.
//
MotorLeftStop();
//
// Make the right motor run half speed forward.
//
MotorRightSpeed(50);
MotorRightDir(1);
MotorRightRun();
//
// Done handling this mode.
//
break;
}
//
// In diagnostic step 4
//
case MODE_DIAG4:
{
//
// Advance to diagnostic step 5.
//
g_eMode = MODE_DIAG5;
//
// Set the light
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -