Theis Rotating Platform Demo code

Dependencies:   X_NUCLEO_IHM01A1_Demo_Code mbed

Fork of Demo_IHM01A1_3-Motors by Arkadi Rafalovich

Committer:
Arkadi
Date:
Thu Feb 25 17:27:42 2016 +0000
Revision:
21:ed054abddfe4
Parent:
17:aae1446c67f4
Child:
23:f9d35a756e79
3 motor activation demo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:e6a49a092e2a 1 /**
Davidroid 0:e6a49a092e2a 2 ******************************************************************************
Davidroid 0:e6a49a092e2a 3 * @file main.cpp
Davidroid 17:aae1446c67f4 4 * @author Davide Aliprandi, STMicroelectronics
Davidroid 0:e6a49a092e2a 5 * @version V1.0.0
Davidroid 0:e6a49a092e2a 6 * @date October 14th, 2015
Davidroid 17:aae1446c67f4 7 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
Davidroid 3:02d9ec4f88b2 8 * Motor Control Expansion Board: control of 2 motors.
Davidroid 0:e6a49a092e2a 9 ******************************************************************************
Davidroid 0:e6a49a092e2a 10 * @attention
Davidroid 0:e6a49a092e2a 11 *
Davidroid 0:e6a49a092e2a 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:e6a49a092e2a 13 *
Davidroid 0:e6a49a092e2a 14 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:e6a49a092e2a 15 * are permitted provided that the following conditions are met:
Davidroid 0:e6a49a092e2a 16 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:e6a49a092e2a 17 * this list of conditions and the following disclaimer.
Davidroid 0:e6a49a092e2a 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:e6a49a092e2a 19 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:e6a49a092e2a 20 * and/or other materials provided with the distribution.
Davidroid 0:e6a49a092e2a 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:e6a49a092e2a 22 * may be used to endorse or promote products derived from this software
Davidroid 0:e6a49a092e2a 23 * without specific prior written permission.
Davidroid 0:e6a49a092e2a 24 *
Davidroid 0:e6a49a092e2a 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:e6a49a092e2a 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:e6a49a092e2a 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:e6a49a092e2a 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:e6a49a092e2a 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:e6a49a092e2a 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:e6a49a092e2a 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:e6a49a092e2a 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:e6a49a092e2a 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:e6a49a092e2a 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:e6a49a092e2a 35 *
Davidroid 0:e6a49a092e2a 36 ******************************************************************************
Davidroid 0:e6a49a092e2a 37 */
Davidroid 0:e6a49a092e2a 38
Davidroid 0:e6a49a092e2a 39
Davidroid 0:e6a49a092e2a 40 /* Includes ------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 41
Davidroid 0:e6a49a092e2a 42 /* mbed specific header files. */
Davidroid 0:e6a49a092e2a 43 #include "mbed.h"
Davidroid 0:e6a49a092e2a 44
Davidroid 0:e6a49a092e2a 45 /* Helper header files. */
Davidroid 0:e6a49a092e2a 46 #include "DevSPI.h"
Davidroid 0:e6a49a092e2a 47
Davidroid 0:e6a49a092e2a 48 /* Component specific header files. */
Davidroid 0:e6a49a092e2a 49 #include "l6474_class.h"
Davidroid 0:e6a49a092e2a 50
Davidroid 0:e6a49a092e2a 51
Davidroid 0:e6a49a092e2a 52 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 53
Davidroid 9:a9e51320aee4 54 /* Number of steps to move. */
Davidroid 9:a9e51320aee4 55 #define STEPS 3200
Davidroid 0:e6a49a092e2a 56
Davidroid 0:e6a49a092e2a 57
Davidroid 0:e6a49a092e2a 58 /* Variables -----------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 59
Davidroid 0:e6a49a092e2a 60 /* Motor Control Component. */
Davidroid 3:02d9ec4f88b2 61 L6474 *motor1;
Davidroid 3:02d9ec4f88b2 62 L6474 *motor2;
Arkadi 21:ed054abddfe4 63 L6474 *motor3;
Davidroid 0:e6a49a092e2a 64
Davidroid 0:e6a49a092e2a 65 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 66
Davidroid 0:e6a49a092e2a 67 int main()
Davidroid 0:e6a49a092e2a 68 {
Davidroid 8:cec4c2c03a27 69 /*----- Initialization. -----*/
Davidroid 8:cec4c2c03a27 70
Davidroid 0:e6a49a092e2a 71 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 72 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 73
Davidroid 9:a9e51320aee4 74 /* Initializing Motor Control Components. */
Davidroid 5:a0268a435bb1 75 motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 16:810667a9f31f 76 motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
Arkadi 21:ed054abddfe4 77 motor3 = new L6474(D2, D8, D5, D6, D10, dev_spi);
Davidroid 14:fcd452b03d1a 78 if (motor1->Init() != COMPONENT_OK)
Davidroid 14:fcd452b03d1a 79 exit(EXIT_FAILURE);
Davidroid 14:fcd452b03d1a 80 if (motor2->Init() != COMPONENT_OK)
Davidroid 14:fcd452b03d1a 81 exit(EXIT_FAILURE);
Arkadi 21:ed054abddfe4 82 if (motor3->Init() != COMPONENT_OK)
Arkadi 21:ed054abddfe4 83 exit(EXIT_FAILURE);
Arkadi 21:ed054abddfe4 84
Arkadi 21:ed054abddfe4 85
Arkadi 21:ed054abddfe4 86
Arkadi 21:ed054abddfe4 87 /*----- Changing motor setting. -----*/
Arkadi 21:ed054abddfe4 88
Arkadi 21:ed054abddfe4 89 /* Setting High Impedance State to update L6474's registers. */
Arkadi 21:ed054abddfe4 90 motor1->SoftHiZ();
Arkadi 21:ed054abddfe4 91 motor2->SoftHiZ();
Arkadi 21:ed054abddfe4 92 motor3->SoftHiZ();
Arkadi 21:ed054abddfe4 93 // Disabling motor
Arkadi 21:ed054abddfe4 94 motor1->Disable();
Arkadi 21:ed054abddfe4 95 motor2->Disable();
Arkadi 21:ed054abddfe4 96 motor3->Disable();
Arkadi 21:ed054abddfe4 97 /* Changing step mode. */
Arkadi 21:ed054abddfe4 98 motor1->SetStepMode(STEP_MODE_1_16);
Arkadi 21:ed054abddfe4 99 motor2->SetStepMode(STEP_MODE_1_16);
Arkadi 21:ed054abddfe4 100 motor3->SetStepMode(STEP_MODE_1_16);
Arkadi 21:ed054abddfe4 101 /* Increasing the torque regulation current to 500mA. */
Arkadi 21:ed054abddfe4 102 motor1->SetParameter(L6474_TVAL, 1250); // Limit 2.0A
Arkadi 21:ed054abddfe4 103 motor2->SetParameter(L6474_TVAL, 1700); // Limit 1.7A
Arkadi 21:ed054abddfe4 104 motor3->SetParameter(L6474_TVAL, 850); // Limit 1.0A
Arkadi 21:ed054abddfe4 105
Arkadi 21:ed054abddfe4 106 /* Max speed to 2400 step/s. */
Arkadi 21:ed054abddfe4 107 motor1->SetMaxSpeed(1000);
Arkadi 21:ed054abddfe4 108 motor2->SetMaxSpeed(1000);
Arkadi 21:ed054abddfe4 109 motor3->SetMaxSpeed(1000);
Arkadi 21:ed054abddfe4 110
Arkadi 21:ed054abddfe4 111 /* Min speed to 200 step/s. */
Arkadi 21:ed054abddfe4 112 motor1->SetMinSpeed(100);
Arkadi 21:ed054abddfe4 113 motor2->SetMinSpeed(100);
Arkadi 21:ed054abddfe4 114 motor3->SetMinSpeed(100);
Arkadi 21:ed054abddfe4 115
Arkadi 21:ed054abddfe4 116 /* set accelerations */
Arkadi 21:ed054abddfe4 117 motor1->SetAcceleration(500);
Arkadi 21:ed054abddfe4 118 motor2->SetAcceleration(500);
Arkadi 21:ed054abddfe4 119 motor3->SetAcceleration(500);
Arkadi 21:ed054abddfe4 120 motor1->SetDeceleration(500);
Arkadi 21:ed054abddfe4 121 motor2->SetDeceleration(500);
Arkadi 21:ed054abddfe4 122 motor3->SetDeceleration(500);
Arkadi 21:ed054abddfe4 123
Arkadi 21:ed054abddfe4 124 // Enabling motor
Arkadi 21:ed054abddfe4 125 motor1->Enable();
Arkadi 21:ed054abddfe4 126 motor2->Enable();
Arkadi 21:ed054abddfe4 127 motor3->Enable();
Arkadi 21:ed054abddfe4 128
Davidroid 0:e6a49a092e2a 129
Davidroid 0:e6a49a092e2a 130 /* Printing to the console. */
Arkadi 21:ed054abddfe4 131 printf("Motor Control Application Example for 3 Motors\r\n\n");
Davidroid 8:cec4c2c03a27 132
Davidroid 8:cec4c2c03a27 133 /*----- Moving. -----*/
Arkadi 21:ed054abddfe4 134
Davidroid 8:cec4c2c03a27 135 /* Moving N steps in the forward direction. */
Arkadi 21:ed054abddfe4 136 motor1->Move(StepperMotor::FWD, STEPS);
Davidroid 9:a9e51320aee4 137 motor2->Move(StepperMotor::FWD, STEPS);
Arkadi 21:ed054abddfe4 138 motor3->Move(StepperMotor::FWD, STEPS);
Davidroid 8:cec4c2c03a27 139 /* Waiting while the motor is active. */
Davidroid 8:cec4c2c03a27 140 motor1->WaitWhileActive();
Davidroid 8:cec4c2c03a27 141 motor2->WaitWhileActive();
Arkadi 21:ed054abddfe4 142 motor3->WaitWhileActive();
Arkadi 21:ed054abddfe4 143
Arkadi 21:ed054abddfe4 144 /* Waiting 2 seconds. */
Davidroid 8:cec4c2c03a27 145 wait_ms(2000);
Arkadi 21:ed054abddfe4 146
Davidroid 8:cec4c2c03a27 147
Davidroid 8:cec4c2c03a27 148 /* Moving N steps in the backward direction. */
Arkadi 21:ed054abddfe4 149 motor1->Move(StepperMotor::BWD, STEPS);
Davidroid 9:a9e51320aee4 150 motor2->Move(StepperMotor::BWD, STEPS);
Arkadi 21:ed054abddfe4 151 motor3->Move(StepperMotor::BWD, STEPS);
Davidroid 8:cec4c2c03a27 152 /* Waiting while the motor is active. */
Davidroid 8:cec4c2c03a27 153 motor1->WaitWhileActive();
Davidroid 8:cec4c2c03a27 154 motor2->WaitWhileActive();
Arkadi 21:ed054abddfe4 155 motor3->WaitWhileActive();
Davidroid 8:cec4c2c03a27 156
Davidroid 8:cec4c2c03a27 157 /* Waiting 2 seconds. */
Davidroid 8:cec4c2c03a27 158 wait_ms(2000);
Davidroid 8:cec4c2c03a27 159
Arkadi 21:ed054abddfe4 160 /* Infinite Loop. */
Arkadi 21:ed054abddfe4 161 while(1)
Arkadi 21:ed054abddfe4 162 {
Arkadi 21:ed054abddfe4 163 /*----- Moving. -----*/
Davidroid 8:cec4c2c03a27 164
Arkadi 21:ed054abddfe4 165 /* Moving N steps in the forward direction. */
Arkadi 21:ed054abddfe4 166 motor1->Move(StepperMotor::FWD, STEPS);
Arkadi 21:ed054abddfe4 167 motor2->Move(StepperMotor::FWD, STEPS);
Arkadi 21:ed054abddfe4 168 motor3->Move(StepperMotor::FWD, STEPS);
Davidroid 8:cec4c2c03a27 169 /* Waiting while the motor is active. */
Davidroid 8:cec4c2c03a27 170 motor1->WaitWhileActive();
Davidroid 8:cec4c2c03a27 171 motor2->WaitWhileActive();
Arkadi 21:ed054abddfe4 172 motor3->WaitWhileActive();
Arkadi 21:ed054abddfe4 173
Arkadi 21:ed054abddfe4 174 /* Moving N steps in the backward direction. */
Arkadi 21:ed054abddfe4 175 motor1->Move(StepperMotor::BWD, STEPS);
Arkadi 21:ed054abddfe4 176 motor2->Move(StepperMotor::BWD, STEPS);
Arkadi 21:ed054abddfe4 177 motor3->Move(StepperMotor::BWD, STEPS);
Davidroid 8:cec4c2c03a27 178 /* Waiting while the motor is active. */
Davidroid 8:cec4c2c03a27 179 motor1->WaitWhileActive();
Davidroid 8:cec4c2c03a27 180 motor2->WaitWhileActive();
Arkadi 21:ed054abddfe4 181 motor3->WaitWhileActive();
Arkadi 21:ed054abddfe4 182
Davidroid 8:cec4c2c03a27 183 /* Waiting 2 seconds. */
Davidroid 8:cec4c2c03a27 184 wait_ms(2000);
Davidroid 0:e6a49a092e2a 185 }
Davidroid 0:e6a49a092e2a 186 }