2 Motores + Joystick
Dependencies: X_NUCLEO_IHM01A1 TextLCD
Fork of HelloWorld_IHM01A1_2Motors_mbedOS by
main.cpp@39:7e30bcc989d3, 2018-05-02 (annotated)
- Committer:
- leogrotti
- Date:
- Wed May 02 13:39:56 2018 +0000
- Revision:
- 39:7e30bcc989d3
- Parent:
- 38:c3d77ff8168a
- Child:
- 40:0b517b49f70d
3 motores configurados
Who changed what in which revision?
User | Revision | Line number | New 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>© 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 | 29:526970c1d998 | 49 | #include "L6474.h" |
Davidroid | 0:e6a49a092e2a | 50 | |
Davidroid | 0:e6a49a092e2a | 51 | |
Davidroid | 0:e6a49a092e2a | 52 | /* Definitions ---------------------------------------------------------------*/ |
Davidroid | 0:e6a49a092e2a | 53 | |
Davidroid | 24:8cb3c4ad055f | 54 | /* Number of steps. */ |
Davidroid | 9:a9e51320aee4 | 55 | #define STEPS 3200 |
Davidroid | 0:e6a49a092e2a | 56 | |
Davidroid | 24:8cb3c4ad055f | 57 | /* Delay in milliseconds. */ |
Davidroid | 24:8cb3c4ad055f | 58 | #define DELAY_1 2000 |
Davidroid | 24:8cb3c4ad055f | 59 | #define DELAY_2 6000 |
Davidroid | 24:8cb3c4ad055f | 60 | #define DELAY_3 8000 |
Davidroid | 24:8cb3c4ad055f | 61 | |
Davidroid | 24:8cb3c4ad055f | 62 | /* Speed in pps (Pulses Per Second). |
Davidroid | 24:8cb3c4ad055f | 63 | In Full Step mode: 1 pps = 1 step/s). |
Davidroid | 24:8cb3c4ad055f | 64 | In 1/N Step Mode: N pps = 1 step/s). */ |
leogrotti | 34:098efd69c86d | 65 | #define SPEED_1 1300 |
leogrotti | 35:a05bef8dd995 | 66 | #define SPEED_2 1300 |
Davidroid | 24:8cb3c4ad055f | 67 | |
Davidroid | 0:e6a49a092e2a | 68 | |
Brunoporto2702 | 32:481674c0f90d | 69 | |
Davidroid | 0:e6a49a092e2a | 70 | /* Variables -----------------------------------------------------------------*/ |
Brunoporto2702 | 32:481674c0f90d | 71 | /* Initialization parameters. */ |
leogrotti | 39:7e30bcc989d3 | 72 | //L6474_init_t init = { |
leogrotti | 39:7e30bcc989d3 | 73 | // 160, /* Acceleration rate in pps^2. Range: (0..+inf). */ |
leogrotti | 39:7e30bcc989d3 | 74 | // 160, /* Deceleration rate in pps^2. Range: (0..+inf). */ |
leogrotti | 39:7e30bcc989d3 | 75 | // 4000, /* Maximum speed in pps. Range: (30..10000]. */ |
leogrotti | 39:7e30bcc989d3 | 76 | // 1300, /* Minimum speed in pps. Range: [30..10000). */ |
leogrotti | 39:7e30bcc989d3 | 77 | // 1500, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */ |
leogrotti | 39:7e30bcc989d3 | 78 | // L6474_OCD_TH_1875mA, /* Overcurrent threshold (OCD_TH register). */ |
leogrotti | 39:7e30bcc989d3 | 79 | // L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */ |
leogrotti | 39:7e30bcc989d3 | 80 | // L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */ |
leogrotti | 39:7e30bcc989d3 | 81 | // L6474_STEP_SEL_1_2, /* Step selection (STEP_SEL field of STEP_MODE register). */ |
leogrotti | 39:7e30bcc989d3 | 82 | // L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */ |
leogrotti | 39:7e30bcc989d3 | 83 | // L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */ |
leogrotti | 39:7e30bcc989d3 | 84 | // L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */ |
leogrotti | 39:7e30bcc989d3 | 85 | // 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */ |
leogrotti | 39:7e30bcc989d3 | 86 | // 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */ |
leogrotti | 39:7e30bcc989d3 | 87 | // L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */ |
leogrotti | 39:7e30bcc989d3 | 88 | // L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */ |
leogrotti | 39:7e30bcc989d3 | 89 | // L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */ |
leogrotti | 39:7e30bcc989d3 | 90 | // L6474_ALARM_EN_OVERCURRENT | |
leogrotti | 39:7e30bcc989d3 | 91 | // L6474_ALARM_EN_THERMAL_SHUTDOWN | |
leogrotti | 39:7e30bcc989d3 | 92 | // L6474_ALARM_EN_THERMAL_WARNING | |
leogrotti | 39:7e30bcc989d3 | 93 | // L6474_ALARM_EN_UNDERVOLTAGE | |
leogrotti | 39:7e30bcc989d3 | 94 | // L6474_ALARM_EN_SW_TURN_ON | |
leogrotti | 39:7e30bcc989d3 | 95 | // L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */ |
leogrotti | 39:7e30bcc989d3 | 96 | //}; |
Brunoporto2702 | 32:481674c0f90d | 97 | |
Brunoporto2702 | 32:481674c0f90d | 98 | Serial pc(USBTX, USBRX); |
Brunoporto2702 | 32:481674c0f90d | 99 | |
Brunoporto2702 | 32:481674c0f90d | 100 | AnalogIn eixo_X(A0); |
Brunoporto2702 | 32:481674c0f90d | 101 | AnalogIn eixo_Y(A1); |
Brunoporto2702 | 32:481674c0f90d | 102 | |
Brunoporto2702 | 32:481674c0f90d | 103 | |
Brunoporto2702 | 32:481674c0f90d | 104 | |
Brunoporto2702 | 32:481674c0f90d | 105 | float x; |
Brunoporto2702 | 32:481674c0f90d | 106 | float y; |
Brunoporto2702 | 32:481674c0f90d | 107 | float w; |
Brunoporto2702 | 32:481674c0f90d | 108 | float u; |
Brunoporto2702 | 32:481674c0f90d | 109 | |
leogrotti | 38:c3d77ff8168a | 110 | //float a; |
leogrotti | 36:0cd4fdbb40af | 111 | |
leogrotti | 35:a05bef8dd995 | 112 | |
leogrotti | 35:a05bef8dd995 | 113 | |
leogrotti | 34:098efd69c86d | 114 | unsigned int minspeed = 1300; |
Brunoporto2702 | 32:481674c0f90d | 115 | |
leogrotti | 34:098efd69c86d | 116 | //int step = 0x08; |
Brunoporto2702 | 32:481674c0f90d | 117 | |
leogrotti | 35:a05bef8dd995 | 118 | unsigned int speed1; |
leogrotti | 35:a05bef8dd995 | 119 | unsigned int speed2; |
leogrotti | 39:7e30bcc989d3 | 120 | unsigned int speed3; |
leogrotti | 36:0cd4fdbb40af | 121 | |
leogrotti | 38:c3d77ff8168a | 122 | //unsigned int pos3; |
Brunoporto2702 | 32:481674c0f90d | 123 | |
Davidroid | 0:e6a49a092e2a | 124 | |
Davidroid | 0:e6a49a092e2a | 125 | /* Motor Control Component. */ |
Davidroid | 3:02d9ec4f88b2 | 126 | L6474 *motor1; |
Davidroid | 3:02d9ec4f88b2 | 127 | L6474 *motor2; |
leogrotti | 39:7e30bcc989d3 | 128 | L6474 *motor3; |
Davidroid | 0:e6a49a092e2a | 129 | |
Davidroid | 0:e6a49a092e2a | 130 | |
Davidroid | 0:e6a49a092e2a | 131 | /* Main ----------------------------------------------------------------------*/ |
Davidroid | 0:e6a49a092e2a | 132 | |
Davidroid | 0:e6a49a092e2a | 133 | int main() |
Davidroid | 0:e6a49a092e2a | 134 | { |
Davidroid | 8:cec4c2c03a27 | 135 | /*----- Initialization. -----*/ |
Davidroid | 8:cec4c2c03a27 | 136 | |
Davidroid | 0:e6a49a092e2a | 137 | /* Initializing SPI bus. */ |
Davidroid | 0:e6a49a092e2a | 138 | DevSPI dev_spi(D11, D12, D13); |
Davidroid | 0:e6a49a092e2a | 139 | |
Davidroid | 9:a9e51320aee4 | 140 | /* Initializing Motor Control Components. */ |
Davidroid | 5:a0268a435bb1 | 141 | motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi); |
Davidroid | 16:810667a9f31f | 142 | motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi); |
leogrotti | 39:7e30bcc989d3 | 143 | motor3 = new L6474(D2, D8, D5, D6, D10, dev_spi); |
Davidroid | 29:526970c1d998 | 144 | if (motor1->init() != COMPONENT_OK) { |
Davidroid | 14:fcd452b03d1a | 145 | exit(EXIT_FAILURE); |
Davidroid | 28:3f17a4152bcf | 146 | } |
Davidroid | 29:526970c1d998 | 147 | if (motor2->init() != COMPONENT_OK) { |
Davidroid | 14:fcd452b03d1a | 148 | exit(EXIT_FAILURE); |
Davidroid | 28:3f17a4152bcf | 149 | } |
leogrotti | 39:7e30bcc989d3 | 150 | if (motor3->init() != COMPONENT_OK) { |
leogrotti | 39:7e30bcc989d3 | 151 | exit(EXIT_FAILURE); |
leogrotti | 39:7e30bcc989d3 | 152 | } |
Davidroid | 0:e6a49a092e2a | 153 | |
leogrotti | 35:a05bef8dd995 | 154 | |
Davidroid | 8:cec4c2c03a27 | 155 | |
leogrotti | 35:a05bef8dd995 | 156 | /*motor1->set_acceleration(160); |
leogrotti | 35:a05bef8dd995 | 157 | motor2->set_acceleration(160); |
leogrotti | 35:a05bef8dd995 | 158 | motor1->set_max_speed(speed1); |
leogrotti | 35:a05bef8dd995 | 159 | motor2->set_max_speed(speed2);*/ |
leogrotti | 36:0cd4fdbb40af | 160 | |
Davidroid | 8:cec4c2c03a27 | 161 | |
leogrotti | 36:0cd4fdbb40af | 162 | |
leogrotti | 36:0cd4fdbb40af | 163 | |
leogrotti | 36:0cd4fdbb40af | 164 | //motor1->set_min_speed(1300); |
leogrotti | 36:0cd4fdbb40af | 165 | //motor2->set_min_speed(1300); |
leogrotti | 36:0cd4fdbb40af | 166 | |
leogrotti | 36:0cd4fdbb40af | 167 | //motor1->set_parameter(L6474_STEP_MODE,L6474_STEP_SEL_1_2); |
leogrotti | 36:0cd4fdbb40af | 168 | //motor2->set_parameter(L6474_STEP_MODE,L6474_STEP_SEL_1_2); |
Davidroid | 8:cec4c2c03a27 | 169 | |
leogrotti | 39:7e30bcc989d3 | 170 | |
leogrotti | 39:7e30bcc989d3 | 171 | |
leogrotti | 39:7e30bcc989d3 | 172 | |
Davidroid | 28:3f17a4152bcf | 173 | while(true) { |
Brunoporto2702 | 32:481674c0f90d | 174 | u = eixo_X.read(); |
Brunoporto2702 | 32:481674c0f90d | 175 | w = eixo_Y.read(); |
Brunoporto2702 | 32:481674c0f90d | 176 | |
Brunoporto2702 | 32:481674c0f90d | 177 | // wait (1.5); |
Brunoporto2702 | 32:481674c0f90d | 178 | // pc.printf ("X: %f \r\n",u); |
Brunoporto2702 | 32:481674c0f90d | 179 | // pc.printf ("Y: %f \r\n",w); |
leogrotti | 39:7e30bcc989d3 | 180 | |
leogrotti | 39:7e30bcc989d3 | 181 | motor3->run(StepperMotor::FWD); |
leogrotti | 39:7e30bcc989d3 | 182 | // motor3->move(StepperMotor::BWD, 100); |
leogrotti | 39:7e30bcc989d3 | 183 | //motor3->wait_while_active(); |
leogrotti | 39:7e30bcc989d3 | 184 | |
leogrotti | 39:7e30bcc989d3 | 185 | // pc.printf ("bwd"); |
leogrotti | 35:a05bef8dd995 | 186 | |
leogrotti | 35:a05bef8dd995 | 187 | |
leogrotti | 34:098efd69c86d | 188 | if (u>0.740) { |
leogrotti | 34:098efd69c86d | 189 | |
Brunoporto2702 | 32:481674c0f90d | 190 | motor1->run(StepperMotor::FWD); |
leogrotti | 39:7e30bcc989d3 | 191 | //wait_ms(DELAY_1); |
leogrotti | 39:7e30bcc989d3 | 192 | |
Brunoporto2702 | 32:481674c0f90d | 193 | speed1 = motor1->get_speed(); |
leogrotti | 34:098efd69c86d | 194 | |
leogrotti | 38:c3d77ff8168a | 195 | printf("speed1 =%d \r\n\n", speed1); |
leogrotti | 38:c3d77ff8168a | 196 | printf("U =%f \r\n\n", u); |
leogrotti | 34:098efd69c86d | 197 | |
Brunoporto2702 | 32:481674c0f90d | 198 | } |
leogrotti | 34:098efd69c86d | 199 | else{ if(u<0.65) { |
leogrotti | 34:098efd69c86d | 200 | |
Brunoporto2702 | 32:481674c0f90d | 201 | motor1->run(StepperMotor::BWD); |
leogrotti | 39:7e30bcc989d3 | 202 | // wait_ms(DELAY_1); |
leogrotti | 39:7e30bcc989d3 | 203 | |
Brunoporto2702 | 32:481674c0f90d | 204 | speed1 = motor1->get_speed(); |
leogrotti | 34:098efd69c86d | 205 | |
leogrotti | 38:c3d77ff8168a | 206 | printf("speed1 =%d \r\n\n", speed1); |
leogrotti | 35:a05bef8dd995 | 207 | |
leogrotti | 38:c3d77ff8168a | 208 | printf("U =%f \r\n\n", u); |
leogrotti | 34:098efd69c86d | 209 | |
Brunoporto2702 | 32:481674c0f90d | 210 | } |
Brunoporto2702 | 32:481674c0f90d | 211 | else |
Brunoporto2702 | 32:481674c0f90d | 212 | motor1->hard_stop(); |
leogrotti | 39:7e30bcc989d3 | 213 | // wait_ms(DELAY_1); |
leogrotti | 39:7e30bcc989d3 | 214 | |
leogrotti | 35:a05bef8dd995 | 215 | speed1 = 0; |
leogrotti | 38:c3d77ff8168a | 216 | printf("speed1 =%d \r\n\n", speed1); |
leogrotti | 34:098efd69c86d | 217 | |
leogrotti | 38:c3d77ff8168a | 218 | printf("U =%f \r\n\n", u); |
leogrotti | 34:098efd69c86d | 219 | |
Brunoporto2702 | 32:481674c0f90d | 220 | } |
Brunoporto2702 | 32:481674c0f90d | 221 | |
leogrotti | 34:098efd69c86d | 222 | if (w>0.76) { |
leogrotti | 34:098efd69c86d | 223 | |
leogrotti | 34:098efd69c86d | 224 | motor2->run(StepperMotor::BWD); |
Brunoporto2702 | 32:481674c0f90d | 225 | speed2 = motor2->get_speed(); |
leogrotti | 35:a05bef8dd995 | 226 | |
leogrotti | 38:c3d77ff8168a | 227 | printf("speed2 =%d \r\n\n", speed2); |
leogrotti | 38:c3d77ff8168a | 228 | printf("W =%f \r\n\n", w); |
leogrotti | 34:098efd69c86d | 229 | |
Brunoporto2702 | 32:481674c0f90d | 230 | } |
leogrotti | 34:098efd69c86d | 231 | else{ if(w<0.67) { |
leogrotti | 34:098efd69c86d | 232 | |
leogrotti | 34:098efd69c86d | 233 | |
leogrotti | 34:098efd69c86d | 234 | motor2->run(StepperMotor::FWD); |
leogrotti | 35:a05bef8dd995 | 235 | speed2 = motor2->get_speed(); |
leogrotti | 35:a05bef8dd995 | 236 | |
leogrotti | 38:c3d77ff8168a | 237 | printf("speed2 =%d \r\n\n", speed2); |
leogrotti | 38:c3d77ff8168a | 238 | printf("W =%f \r\n\n", w); |
leogrotti | 34:098efd69c86d | 239 | |
Brunoporto2702 | 32:481674c0f90d | 240 | } |
Brunoporto2702 | 32:481674c0f90d | 241 | else |
Brunoporto2702 | 32:481674c0f90d | 242 | motor2->hard_stop(); |
leogrotti | 35:a05bef8dd995 | 243 | speed2 = 0; |
leogrotti | 38:c3d77ff8168a | 244 | printf("speed2 =%d \r\n\n", speed2); |
leogrotti | 38:c3d77ff8168a | 245 | printf("W =%f \r\n\n", w); |
leogrotti | 34:098efd69c86d | 246 | |
Brunoporto2702 | 32:481674c0f90d | 247 | } |
leogrotti | 36:0cd4fdbb40af | 248 | |
leogrotti | 36:0cd4fdbb40af | 249 | |
Davidroid | 0:e6a49a092e2a | 250 | } |
Brunoporto2702 | 32:481674c0f90d | 251 | } |