Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 3:50e030d90648
- Parent:
- 2:050f12806bc5
- Child:
- 4:d452a7e3461a
diff -r 050f12806bc5 -r 50e030d90648 main.cpp --- a/main.cpp Fri Jun 17 11:18:41 2016 +0000 +++ b/main.cpp Fri Jun 17 11:45:23 2016 +0000 @@ -13,7 +13,8 @@ DigitalOut myled(LED1); // Blinking LED // --- Define the Foor PINs used for Motor drive ----- -Motor(PA_9, PC_7, PB_6, PA_7) +Motor motorBox(PA_9, PC_7, PB_6, PA_7) +// Motor motorDancer(PA_9, PC_7, PB_6, PA_7) // --- Define PC_8 as the output of PWM use for Tones ----- Buzzer buzzer(PC_8); @@ -23,104 +24,6 @@ // --- Motor Routine -void StartMotor() -{ - MotorIndex = 0; - MPh0 = 1; MPh1 = 0; MPh2 = 0; MPh3 = 0; -} -void RightMotor() // Move the Motor one step Right -{ - const int RPh0[4] = {0, 1, 0, 0}; - const int RPh1[4] = {0, 0, 1, 0}; - const int RPh2[4] = {0, 0, 0, 1}; - const int RPh3[4] = {1, 0, 0, 0}; - MPh0 = RPh0[MotorIndex]; MPh1 = RPh1[MotorIndex]; MPh2 = RPh2[MotorIndex]; MPh3 = RPh3[MotorIndex]; - if (MotorIndex<3) MotorIndex++; - else MotorIndex = 0; -} -void LeftMotor() // Move the Motor one step Right -{ - const int LPh0[4] = {0, 0, 1, 0}; - const int LPh1[4] = {0, 1, 0, 0}; - const int LPh2[4] = {1, 0, 0, 0}; - const int LPh3[4] = {0, 0, 0, 1}; - MPh0 = LPh0[MotorIndex]; MPh1 = LPh1[MotorIndex]; MPh2 = LPh2[MotorIndex]; MPh3 = LPh3[MotorIndex]; - if (MotorIndex<3) MotorIndex++; - else MotorIndex = 0; -} - -void RunMotor() // Move the Motor in the user direction -{ - if (MotorDir==d_clock) RightMotor(); - else LeftMotor(); -} - -void ProcessMotorStateMachine() -{ - if (MotorState==Motor_IDLE) { - uint32_t led = 0; - if (MotorCommand == k_wire) { - // Start the Wiring - StartMotor(); - led = 1; - MotorState = Motor_RUN; - } - else if (MotorCommand == k_zero) { - // Start zeroing the Motor - StartMotor(); - led = 1; - MotorState = Motor_ZERO; - } - MotorCommand = k_nop; - myled = led; // LED is on when motor in use - } - else if (MotorState==Motor_RUN) { - // Action always performed in that state - if (NumSteps>0) { - RunMotor(); - NumSteps--; - } - // Check next state - if (MotorCommand == k_pause) { - MotorState = Motor_PAUSE; - } - else if ((MotorCommand == k_stop)||(NumSteps<=0)) { - StopMotor(); - MotorState = Motor_IDLE; - } - MotorCommand = k_nop; - } - else if (MotorState==Motor_PAUSE) { - if (MotorCommand == k_stop) { - StopMotor(); - NumSteps=0; - MotorState = Motor_IDLE; - } - else if (MotorCommand == k_restart) { - MotorState = Motor_RUN; - } - MotorCommand = k_nop; - } - else if (MotorState==Motor_ZERO) { - MotorCommand = k_nop; - } -} - -void TestMotor() // Just to check that it make a full taurn back and forth -{ - int i; - StartMotor(); - for (i=0; i<MotorFullTurn; i++) { - wait(0.005); - RightMotor(); - } - wait(0.5); - for (i=0; i<MotorFullTurn; i++) { - wait(0.005); - LeftMotor(); - } - StopMotor(); -} void help() // Display list of Commands { DEBUG("List of commands:\n\r"); @@ -139,17 +42,8 @@ DEBUG(" [space] --> print remaining Motor steps\n\r"); } -void end_wiring() // Display message and Play a song when wirring is completed -{ - DEBUG("--- SUCCESS: Coils wiring completed ---\n\r"); - // Play a single Song - pMusic= new Music(song_lightmyfire); - pMusic->play(&buzzer); - buzzer.tone(&la); - delete(pMusic); -} - int main() { + uint32_t NumberOfTurns; myled = 1; // To see something is alive DEBUG("\n\n\r"); DEBUG("------------------------------------------\n\r"); @@ -159,17 +53,7 @@ DEBUG("------------------------------------------\n\r"); wait(5); // Some delay myled = 0; // Real stuff starts here - // - // Connect Interrupt routine in which the motor and all the state machine is performed - // - MotorDir = d_clock; // Default direction is clockwise - MotorState = Motor_IDLE; // Default state is IDLE - MotorCommand = k_nop; // Default command is NOP - MotorStepTime = 10000; // value in micro second for one step - MotorFullTurn = 2140; // Initial Calibration - NumSteps = 0; // Default - MotorSystemTick.attach_us(&ProcessMotorStateMachine, MotorStepTime); - // + // while(1) { char command; // Command to execute uint32_t TimeToRunSec, TimeToRunMin; @@ -181,13 +65,9 @@ case 'z': MotorCommand = k_zero; break; case 'k': TestMotor(); break; case 'n': - pc_uart.scanf("%d", &NumWires); - NumSteps = NumWires*MotorFullTurn; - TimeToRunSec = (NumSteps * (MotorStepTime / 1000))/1000; - TimeToRunMin = TimeToRunSec / 60; - TimeToRunSec %= 60; - DEBUG("%d --> Step = %d; Time = %d,%2d (min, sec)\n\r", NumWires, NumSteps, TimeToRunMin, TimeToRunSec); - break; + pc_uart.scanf("%d", &NumberOfTurns); + MotorBox->TurnMotor(NumberOfTurns); + break; case 'c': MotorDir = d_clock; break; case 'a': MotorDir = d_anti; break; case 'm': DEBUG(" [ not ready yet ]\n\r"); break;