Controla aX-NUCLEO-IHM01A1 a partir do Joystick Shield V2.4

Dependencies:   mbed

Committer:
afm76
Date:
Sun Mar 18 23:18:10 2018 +0000
Revision:
0:4643b73902b7
em desenvolvimento

Who changed what in which revision?

UserRevisionLine numberNew contents of line
afm76 0:4643b73902b7 1
afm76 0:4643b73902b7 2 /* Includes ------------------------------------------------------------------*/
afm76 0:4643b73902b7 3
afm76 0:4643b73902b7 4 /* mbed specific header files. */
afm76 0:4643b73902b7 5 #include "mbed.h"
afm76 0:4643b73902b7 6
afm76 0:4643b73902b7 7 /* Helper header files. */
afm76 0:4643b73902b7 8 #include "DevSPI.h"
afm76 0:4643b73902b7 9
afm76 0:4643b73902b7 10 /* Component specific header files. */
afm76 0:4643b73902b7 11 #include "L6474.h"
afm76 0:4643b73902b7 12
afm76 0:4643b73902b7 13
afm76 0:4643b73902b7 14 /* Variables -----------------------------------------------------------------*/
afm76 0:4643b73902b7 15
afm76 0:4643b73902b7 16 /* Initialization parameters. */
afm76 0:4643b73902b7 17 L6474_init_t init = {
afm76 0:4643b73902b7 18 160, /* Acceleration rate in pps^2. Range: (0..+inf). */
afm76 0:4643b73902b7 19 160, /* Deceleration rate in pps^2. Range: (0..+inf). */
afm76 0:4643b73902b7 20 1600, /* Maximum speed in pps. Range: (30..10000]. */
afm76 0:4643b73902b7 21 800, /* Minimum speed in pps. Range: [30..10000). */
afm76 0:4643b73902b7 22 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
afm76 0:4643b73902b7 23 L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */
afm76 0:4643b73902b7 24 L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
afm76 0:4643b73902b7 25 L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */
afm76 0:4643b73902b7 26 L6474_STEP_SEL_1_16, /* Step selection (STEP_SEL field of STEP_MODE register). */
afm76 0:4643b73902b7 27 L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */
afm76 0:4643b73902b7 28 L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
afm76 0:4643b73902b7 29 L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
afm76 0:4643b73902b7 30 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
afm76 0:4643b73902b7 31 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
afm76 0:4643b73902b7 32 L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */
afm76 0:4643b73902b7 33 L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */
afm76 0:4643b73902b7 34 L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
afm76 0:4643b73902b7 35 L6474_ALARM_EN_OVERCURRENT |
afm76 0:4643b73902b7 36 L6474_ALARM_EN_THERMAL_SHUTDOWN |
afm76 0:4643b73902b7 37 L6474_ALARM_EN_THERMAL_WARNING |
afm76 0:4643b73902b7 38 L6474_ALARM_EN_UNDERVOLTAGE |
afm76 0:4643b73902b7 39 L6474_ALARM_EN_SW_TURN_ON |
afm76 0:4643b73902b7 40 L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */
afm76 0:4643b73902b7 41 };
afm76 0:4643b73902b7 42
afm76 0:4643b73902b7 43 /* Motor Control Component. */
afm76 0:4643b73902b7 44 L6474 *motor;
afm76 0:4643b73902b7 45
afm76 0:4643b73902b7 46
afm76 0:4643b73902b7 47 /* Functions -----------------------------------------------------------------*/
afm76 0:4643b73902b7 48
afm76 0:4643b73902b7 49 /**
afm76 0:4643b73902b7 50 * @brief This is an example of user handler for the flag interrupt.
afm76 0:4643b73902b7 51 * Empty parts can be implemented by the user upon needs.
afm76 0:4643b73902b7 52 * @param None.
afm76 0:4643b73902b7 53 * @retval None.
afm76 0:4643b73902b7 54 * @note If needed, implement it, and then attach and enable it:
afm76 0:4643b73902b7 55 * + motor->AttachFlagIRQ(&FlagIRQHandler);
afm76 0:4643b73902b7 56 * + motor->EnableFlagIRQ();
afm76 0:4643b73902b7 57 * To disable it:
afm76 0:4643b73902b7 58 * + motor->DisbleFlagIRQ();
afm76 0:4643b73902b7 59 */
afm76 0:4643b73902b7 60 void FlagIRQHandler(void)
afm76 0:4643b73902b7 61 {
afm76 0:4643b73902b7 62 /* Set ISR flag. */
afm76 0:4643b73902b7 63 motor->isr_flag = TRUE;
afm76 0:4643b73902b7 64
afm76 0:4643b73902b7 65 /* Get the value of the status register. */
afm76 0:4643b73902b7 66 unsigned int status = motor->get_status();
afm76 0:4643b73902b7 67
afm76 0:4643b73902b7 68 /* Check HIZ flag: if set, power brigdes are disabled. */
afm76 0:4643b73902b7 69 if ((status & L6474_STATUS_HIZ) == L6474_STATUS_HIZ)
afm76 0:4643b73902b7 70 { /* HIZ state. Action to be customized. */ }
afm76 0:4643b73902b7 71
afm76 0:4643b73902b7 72 /* Check direction. */
afm76 0:4643b73902b7 73 if ((status & L6474_STATUS_DIR) == L6474_STATUS_DIR)
afm76 0:4643b73902b7 74 { /* Forward direction is set. Action to be customized. */ }
afm76 0:4643b73902b7 75 else
afm76 0:4643b73902b7 76 { /* Backward direction is set. Action to be customized. */ }
afm76 0:4643b73902b7 77
afm76 0:4643b73902b7 78 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
afm76 0:4643b73902b7 79 /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */
afm76 0:4643b73902b7 80 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD)
afm76 0:4643b73902b7 81 { /* Command received by SPI can't be performed. Action to be customized. */ }
afm76 0:4643b73902b7 82
afm76 0:4643b73902b7 83 /* Check WRONG_CMD flag: if set, the command does not exist. */
afm76 0:4643b73902b7 84 if ((status & L6474_STATUS_WRONG_CMD) == L6474_STATUS_WRONG_CMD)
afm76 0:4643b73902b7 85 { /* The command received by SPI does not exist. Action to be customized. */ }
afm76 0:4643b73902b7 86
afm76 0:4643b73902b7 87 /* Check UVLO flag: if not set, there is an undervoltage lock-out. */
afm76 0:4643b73902b7 88 if ((status & L6474_STATUS_UVLO) == 0)
afm76 0:4643b73902b7 89 { /* Undervoltage lock-out. Action to be customized. */ }
afm76 0:4643b73902b7 90
afm76 0:4643b73902b7 91 /* Check TH_WRN flag: if not set, the thermal warning threshold is reached. */
afm76 0:4643b73902b7 92 if ((status & L6474_STATUS_TH_WRN) == 0)
afm76 0:4643b73902b7 93 { /* Thermal warning threshold is reached. Action to be customized. */ }
afm76 0:4643b73902b7 94
afm76 0:4643b73902b7 95 /* Check TH_SHD flag: if not set, the thermal shut down threshold is reached. */
afm76 0:4643b73902b7 96 if ((status & L6474_STATUS_TH_SD) == 0)
afm76 0:4643b73902b7 97 { /* Thermal shut down threshold is reached. Action to be customized. */ }
afm76 0:4643b73902b7 98
afm76 0:4643b73902b7 99 /* Check OCD flag: if not set, there is an overcurrent detection. */
afm76 0:4643b73902b7 100 if ((status & L6474_STATUS_OCD) == 0)
afm76 0:4643b73902b7 101 { /* Overcurrent detection. Action to be customized. */ }
afm76 0:4643b73902b7 102
afm76 0:4643b73902b7 103 /* Reset ISR flag. */
afm76 0:4643b73902b7 104 motor->isr_flag = FALSE;
afm76 0:4643b73902b7 105 }
afm76 0:4643b73902b7 106
afm76 0:4643b73902b7 107 /**
afm76 0:4643b73902b7 108 * @brief This is an example of user handler for the errors.
afm76 0:4643b73902b7 109 * @param error error-code.
afm76 0:4643b73902b7 110 * @retval None
afm76 0:4643b73902b7 111 * @note If needed, implement it, and then attach it:
afm76 0:4643b73902b7 112 * + motor->AttachErrorHandler(&ErrorHandler);
afm76 0:4643b73902b7 113 */
afm76 0:4643b73902b7 114 void ErrorHandler(uint16_t error)
afm76 0:4643b73902b7 115 {
afm76 0:4643b73902b7 116 /* Printing to the console. */
afm76 0:4643b73902b7 117 printf("Error: %d.\r\n", error);
afm76 0:4643b73902b7 118
afm76 0:4643b73902b7 119 /* Aborting the program. */
afm76 0:4643b73902b7 120 exit(EXIT_FAILURE);
afm76 0:4643b73902b7 121 }
afm76 0:4643b73902b7 122
afm76 0:4643b73902b7 123
afm76 0:4643b73902b7 124 /* Main ----------------------------------------------------------------------*/
afm76 0:4643b73902b7 125
afm76 0:4643b73902b7 126 int main()
afm76 0:4643b73902b7 127 {
afm76 0:4643b73902b7 128 /*----- Initialization. -----*/
afm76 0:4643b73902b7 129
afm76 0:4643b73902b7 130 /* Initializing SPI bus. */
afm76 0:4643b73902b7 131 DevSPI dev_spi(D11, D12, D13);
afm76 0:4643b73902b7 132
afm76 0:4643b73902b7 133 /* Initializing Motor Control Component. */
afm76 0:4643b73902b7 134 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
afm76 0:4643b73902b7 135 if (motor->init(&init) != COMPONENT_OK) {
afm76 0:4643b73902b7 136 exit(EXIT_FAILURE);
afm76 0:4643b73902b7 137 }
afm76 0:4643b73902b7 138
afm76 0:4643b73902b7 139 /* Attaching and enabling the user handler for the flag interrupt. */
afm76 0:4643b73902b7 140 motor->attach_flag_irq(&FlagIRQHandler);
afm76 0:4643b73902b7 141 motor->enable_flag_irq();
afm76 0:4643b73902b7 142
afm76 0:4643b73902b7 143 /* Printing to the console. */
afm76 0:4643b73902b7 144 printf("Stepper Motor Control with Joystick Example\r\n\n");
afm76 0:4643b73902b7 145
afm76 0:4643b73902b7 146 int speed;
afm76 0:4643b73902b7 147 ////////////////////////////////////////////////////////////////////////////////
afm76 0:4643b73902b7 148 int a,b,c,d,e,f,k;
afm76 0:4643b73902b7 149 int i;
afm76 0:4643b73902b7 150 int x,y;
afm76 0:4643b73902b7 151 float u,w;
afm76 0:4643b73902b7 152
afm76 0:4643b73902b7 153 //DigitalIn botao_A(D2);
afm76 0:4643b73902b7 154 DigitalIn botao_B(D3);
afm76 0:4643b73902b7 155 DigitalIn botao_C(D4);
afm76 0:4643b73902b7 156 DigitalIn botao_D(D5);
afm76 0:4643b73902b7 157 DigitalIn botao_E(D6);
afm76 0:4643b73902b7 158 //DigitalIn botao_F(D7);
afm76 0:4643b73902b7 159 //DigitalIn botao_K(D8);
afm76 0:4643b73902b7 160
afm76 0:4643b73902b7 161 AnalogIn eixo_X(A0);
afm76 0:4643b73902b7 162 AnalogIn eixo_Y(A1);
afm76 0:4643b73902b7 163
afm76 0:4643b73902b7 164 DigitalOut myled(LED1);
afm76 0:4643b73902b7 165
afm76 0:4643b73902b7 166 Serial pc(USBTX, USBRX);
afm76 0:4643b73902b7 167
afm76 0:4643b73902b7 168 pc.baud(9600);
afm76 0:4643b73902b7 169
afm76 0:4643b73902b7 170 a=b=c=d=e=f=k=0; //a=0,b=0,c=0,d=0,e=0,f=0,k=0;
afm76 0:4643b73902b7 171 ////////////////////////////////////////////////////////////////////////////////
afm76 0:4643b73902b7 172
afm76 0:4643b73902b7 173 /*----- Changing step mode to full step mode. -----*/
afm76 0:4643b73902b7 174
afm76 0:4643b73902b7 175 /* Printing to the console. */
afm76 0:4643b73902b7 176 printf("--> Changing step mode to full step mode.\r\n");
afm76 0:4643b73902b7 177
afm76 0:4643b73902b7 178 /* Selecting full step mode. */
afm76 0:4643b73902b7 179 if (!motor->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_FULL)) {
afm76 0:4643b73902b7 180 printf(" Step Mode not allowed.\r\n");
afm76 0:4643b73902b7 181 }
afm76 0:4643b73902b7 182
afm76 0:4643b73902b7 183 /* Setting speed and acceleration to be consistent with full step mode. */
afm76 0:4643b73902b7 184 motor->set_max_speed(1000);
afm76 0:4643b73902b7 185 motor->set_min_speed(50);
afm76 0:4643b73902b7 186 motor->set_acceleration(10);
afm76 0:4643b73902b7 187 motor->set_deceleration(10);
afm76 0:4643b73902b7 188
afm76 0:4643b73902b7 189 }
afm76 0:4643b73902b7 190 /////////////////////////////////////////////////////////////////////////////////////
afm76 0:4643b73902b7 191
afm76 0:4643b73902b7 192 ------------------------------------------------------------------------------*/
afm76 0:4643b73902b7 193
afm76 0:4643b73902b7 194
afm76 0:4643b73902b7 195 //cls();
afm76 0:4643b73902b7 196 pc.printf("\f\n\r\t\t\t\t\t\tHello Analog World!!!");
afm76 0:4643b73902b7 197 pc.printf("\n\rStart\tA=%d,\tB=%d,\tC=%d,\tD=%d,\tE=%d,\tF=%d,\tK=%d,\tEixos<int>\tEixos<float>\tEixos<status>",a,b,c,d,e,f,k);
afm76 0:4643b73902b7 198 pc.printf("\n\rBotao\tA\tB\tC\tD\tE\tF\tK\tx\ty\tX\tY\n");
afm76 0:4643b73902b7 199
afm76 0:4643b73902b7 200
afm76 0:4643b73902b7 201 while(1) {
afm76 0:4643b73902b7 202
afm76 0:4643b73902b7 203 if (botao_A == 0) { // Botão usuário pressionado
afm76 0:4643b73902b7 204 a++;
afm76 0:4643b73902b7 205 }
afm76 0:4643b73902b7 206 if (botao_B == 0) { // Botão externo pressionado
afm76 0:4643b73902b7 207 b++;
afm76 0:4643b73902b7 208 }
afm76 0:4643b73902b7 209 if (botao_C == 0) { // Botão usuário pressionado
afm76 0:4643b73902b7 210 c++;
afm76 0:4643b73902b7 211 }
afm76 0:4643b73902b7 212 if (botao_D == 0) { // Botão usuário pressionado
afm76 0:4643b73902b7 213 d++;
afm76 0:4643b73902b7 214 }
afm76 0:4643b73902b7 215 if (botao_E == 0) { // Botão usuário pressionado
afm76 0:4643b73902b7 216 e++;
afm76 0:4643b73902b7 217 }
afm76 0:4643b73902b7 218 if (botao_F == 0) { // Botão usuário pressionado
afm76 0:4643b73902b7 219 f++;
afm76 0:4643b73902b7 220 }
afm76 0:4643b73902b7 221 if (botao_K == 0) { // Botão usuário pressionado
afm76 0:4643b73902b7 222 k++;
afm76 0:4643b73902b7 223 }
afm76 0:4643b73902b7 224 //x=(int)eixo_X.read();
afm76 0:4643b73902b7 225 u=eixo_X.read();
afm76 0:4643b73902b7 226 //x=(int)u;
afm76 0:4643b73902b7 227 x=eixo_X.read()*255; //Converte uma leitura do ADC em float para formato int e armazena na variável y.
afm76 0:4643b73902b7 228
afm76 0:4643b73902b7 229 //w=eixo_Y.read();
afm76 0:4643b73902b7 230
afm76 0:4643b73902b7 231 //y=eixo_Y.read()*255; //Converte uma leitura do ADC em float para formato int e armazena na variável y.
afm76 0:4643b73902b7 232
afm76 0:4643b73902b7 233 pc.printf("\rValor\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%X \t%X \t%2.3f\t%2.3f",a,b,c,d,e,f,k,x,y,eixo_X.read(),eixo_Y.read());
afm76 0:4643b73902b7 234
afm76 0:4643b73902b7 235 //Detecta a faixa em que o cursor do pot se encontra:
afm76 0:4643b73902b7 236 if (u>0.505){pc.printf(" X FWD ");
afm76 0:4643b73902b7 237 /* Setting the speed. */
afm76 0:4643b73902b7 238 motor->set_max_speed(x);
afm76 0:4643b73902b7 239 /* Requesting to run Forward. */
afm76 0:4643b73902b7 240 motor->run(StepperMotor::FWD);} //pot > 0.505V (comanda acionamento X proporcional para FRENTE);
afm76 0:4643b73902b7 241 else{if (u<0.495)
afm76 0:4643b73902b7 242 {pc.printf(" X BWD ");
afm76 0:4643b73902b7 243 /* Setting the speed. */
afm76 0:4643b73902b7 244 motor->set_max_speed(x);
afm76 0:4643b73902b7 245 /* Requesting to run Backward. */
afm76 0:4643b73902b7 246 motor->run(StepperMotor::BWD);} //pot < 0.495V (comanda acionamento X proporcional para TRAS);
afm76 0:4643b73902b7 247 else{pc.printf(" X STOP"); //0.495V < pot < 0.505V (comanda acionamento X para permanecer PARADO).
afm76 0:4643b73902b7 248 /* Requesting to immediatly stop. */
afm76 0:4643b73902b7 249 motor->hard_stop();}
afm76 0:4643b73902b7 250 };
afm76 0:4643b73902b7 251
afm76 0:4643b73902b7 252 wait(0.2);
afm76 0:4643b73902b7 253 }
afm76 0:4643b73902b7 254 }
afm76 0:4643b73902b7 255