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.
Dependencies: BufferedSerial X_NUCLEO_IHM01A1_Disabled_Control mbed
Fork of Demo_IHM01A1_3-Motors by
Diff: main.cpp
- Revision:
- 21:ed054abddfe4
- Parent:
- 17:aae1446c67f4
- Child:
- 23:1bcf834fb859
--- a/main.cpp Mon Feb 15 16:04:45 2016 +0000
+++ b/main.cpp Thu Feb 25 17:27:42 2016 +0000
@@ -60,7 +60,7 @@
/* Motor Control Component. */
L6474 *motor1;
L6474 *motor2;
-
+L6474 *motor3;
/* Main ----------------------------------------------------------------------*/
@@ -74,219 +74,113 @@
/* Initializing Motor Control Components. */
motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
+ motor3 = new L6474(D2, D8, D5, D6, D10, dev_spi);
if (motor1->Init() != COMPONENT_OK)
exit(EXIT_FAILURE);
if (motor2->Init() != COMPONENT_OK)
exit(EXIT_FAILURE);
+ if (motor3->Init() != COMPONENT_OK)
+ exit(EXIT_FAILURE);
+
+
+
+ /*----- Changing motor setting. -----*/
+
+ /* Setting High Impedance State to update L6474's registers. */
+ motor1->SoftHiZ();
+ motor2->SoftHiZ();
+ motor3->SoftHiZ();
+ // Disabling motor
+ motor1->Disable();
+ motor2->Disable();
+ motor3->Disable();
+ /* Changing step mode. */
+ motor1->SetStepMode(STEP_MODE_1_16);
+ motor2->SetStepMode(STEP_MODE_1_16);
+ motor3->SetStepMode(STEP_MODE_1_16);
+ /* Increasing the torque regulation current to 500mA. */
+ motor1->SetParameter(L6474_TVAL, 1250); // Limit 2.0A
+ motor2->SetParameter(L6474_TVAL, 1700); // Limit 1.7A
+ motor3->SetParameter(L6474_TVAL, 850); // Limit 1.0A
+
+ /* Max speed to 2400 step/s. */
+ motor1->SetMaxSpeed(1000);
+ motor2->SetMaxSpeed(1000);
+ motor3->SetMaxSpeed(1000);
+
+ /* Min speed to 200 step/s. */
+ motor1->SetMinSpeed(100);
+ motor2->SetMinSpeed(100);
+ motor3->SetMinSpeed(100);
+
+ /* set accelerations */
+ motor1->SetAcceleration(500);
+ motor2->SetAcceleration(500);
+ motor3->SetAcceleration(500);
+ motor1->SetDeceleration(500);
+ motor2->SetDeceleration(500);
+ motor3->SetDeceleration(500);
+
+ // Enabling motor
+ motor1->Enable();
+ motor2->Enable();
+ motor3->Enable();
+
/* Printing to the console. */
- printf("Motor Control Application Example for 2 Motors\r\n\n");
-
+ printf("Motor Control Application Example for 3 Motors\r\n\n");
/*----- Moving. -----*/
-
- /* Printing to the console. */
- printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
-
+
/* Moving N steps in the forward direction. */
- motor1->Move(StepperMotor::FWD, STEPS >> 1);
+ motor1->Move(StepperMotor::FWD, STEPS);
motor2->Move(StepperMotor::FWD, STEPS);
-
+ motor3->Move(StepperMotor::FWD, STEPS);
/* Waiting while the motor is active. */
motor1->WaitWhileActive();
motor2->WaitWhileActive();
-
- /* Getting current position. */
- int position1 = motor1->GetPosition();
- int position2 = motor2->GetPosition();
-
- /* Printing to the console. */
- printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
-
- /* Waiting 2 seconds. */
+ motor3->WaitWhileActive();
+
+ /* Waiting 2 seconds. */
wait_ms(2000);
-
-
- /*----- Moving. -----*/
-
- /* Printing to the console. */
- printf("--> Moving backward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
-
+
/* Moving N steps in the backward direction. */
- motor1->Move(StepperMotor::BWD, STEPS >> 1);
+ motor1->Move(StepperMotor::BWD, STEPS);
motor2->Move(StepperMotor::BWD, STEPS);
-
+ motor3->Move(StepperMotor::BWD, STEPS);
/* Waiting while the motor is active. */
motor1->WaitWhileActive();
motor2->WaitWhileActive();
-
- /* Getting current position. */
- position1 = motor1->GetPosition();
- position2 = motor2->GetPosition();
-
- /* Printing to the console. */
- printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
- printf("--> Setting Home.\r\n");
-
- /* Setting the current position to be the home position. */
- motor1->SetHome();
- motor2->SetHome();
-
- /* Waiting 2 seconds. */
- wait_ms(2000);
-
-
- /*----- Going to a specified position. -----*/
-
- /* Printing to the console. */
- printf("--> Going to position: M1 %d, M2 %d.\r\n", STEPS, STEPS >> 1);
-
- /* Requesting to go to a specified position. */
- motor1->GoTo(STEPS);
- motor2->GoTo(STEPS >> 1);
-
- /* Waiting while the motor is active. */
- motor1->WaitWhileActive();
- motor2->WaitWhileActive();
-
- /* Getting current position. */
- position1 = motor1->GetPosition();
- position2 = motor2->GetPosition();
-
- /* Printing to the console. */
- printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
+ motor3->WaitWhileActive();
/* Waiting 2 seconds. */
wait_ms(2000);
+ /* Infinite Loop. */
+ while(1)
+ {
+ /*----- Moving. -----*/
- /*----- Going Home. -----*/
-
- /* Printing to the console. */
- printf("--> Going Home.\r\n");
-
- /* Requesting to go to home. */
- motor1->GoHome();
- motor2->GoHome();
-
+ /* Moving N steps in the forward direction. */
+ motor1->Move(StepperMotor::FWD, STEPS);
+ motor2->Move(StepperMotor::FWD, STEPS);
+ motor3->Move(StepperMotor::FWD, STEPS);
/* Waiting while the motor is active. */
motor1->WaitWhileActive();
motor2->WaitWhileActive();
-
- /* Getting current position. */
- position1 = motor1->GetPosition();
- position2 = motor2->GetPosition();
-
- /* Printing to the console. */
- printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
-
- /* Waiting 2 seconds. */
- wait_ms(2000);
-
-
- /*----- Running. -----*/
-
- /* Printing to the console. */
- printf("--> M1 running backward, M2 running forward.\r\n");
-
- /* Requesting to run backward. */
- motor1->Run(StepperMotor::BWD);
- motor2->Run(StepperMotor::FWD);
-
- /* Waiting until delay has expired. */
- wait_ms(6000);
-
- /* Getting current speed. */
- int speed1 = motor1->GetSpeed();
- int speed2 = motor2->GetSpeed();
-
- /* Printing to the console. */
- printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
-
- /*----- Increasing the speed while running. -----*/
-
- /* Printing to the console. */
- printf("--> Increasing the speed while running.\r\n");
-
- /* Increasing speed to 2400 step/s. */
- motor1->SetMaxSpeed(2400);
- motor2->SetMaxSpeed(2400);
-
- /* Waiting until delay has expired. */
- wait_ms(6000);
-
- /* Getting current speed. */
- speed1 = motor1->GetSpeed();
- speed2 = motor2->GetSpeed();
-
- /* Printing to the console. */
- printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
-
-
- /*----- Decreasing the speed while running. -----*/
-
- /* Printing to the console. */
- printf("--> Decreasing the speed while running.\r\n");
-
- /* Decreasing speed to 1200 step/s. */
- motor1->SetMaxSpeed(1200);
- motor2->SetMaxSpeed(1200);
-
- /* Waiting until delay has expired. */
- wait_ms(8000);
-
- /* Getting current speed. */
- speed1 = motor1->GetSpeed();
- speed2 = motor2->GetSpeed();
-
- /* Printing to the console. */
- printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
-
-
- /*----- Requiring hard-stop while running. -----*/
-
- /* Printing to the console. */
- printf("--> Requiring hard-stop while running.\r\n");
-
- /* Requesting to immediatly stop. */
- motor1->HardStop();
- motor2->HardStop();
-
+ motor3->WaitWhileActive();
+
+ /* Moving N steps in the backward direction. */
+ motor1->Move(StepperMotor::BWD, STEPS);
+ motor2->Move(StepperMotor::BWD, STEPS);
+ motor3->Move(StepperMotor::BWD, STEPS);
/* Waiting while the motor is active. */
motor1->WaitWhileActive();
motor2->WaitWhileActive();
-
+ motor3->WaitWhileActive();
+
/* Waiting 2 seconds. */
wait_ms(2000);
-
-
- /*----- Infinite Loop. -----*/
-
- /* Printing to the console. */
- printf("--> Infinite Loop...\r\n");
-
- /* Setting the current position to be the home position. */
- motor1->SetHome();
- motor2->SetHome();
-
- /* Infinite Loop. */
- while(1)
- {
- /* Requesting to go to a specified position. */
- motor1->GoTo(STEPS >> 1);
- motor2->GoTo(- (STEPS >> 1));
-
- /* Waiting while the motor is active. */
- motor1->WaitWhileActive();
- motor2->WaitWhileActive();
-
- /* Requesting to go to a specified position. */
- motor1->GoTo(- (STEPS >> 1));
- motor2->GoTo(STEPS >> 1);
-
- /* Waiting while the motor is active. */
- motor1->WaitWhileActive();
- motor2->WaitWhileActive();
}
}
