APS2
Dependencies: mbed X_NUCLEO_IHM01A1
main.cpp@38:3d32ce11e4d8, 2021-10-24 (annotated)
- Committer:
- stephanie_liu
- Date:
- Sun Oct 24 22:17:48 2021 +0000
- Revision:
- 38:3d32ce11e4d8
- Parent:
- 35:2b44ed4ec7a0
APS2 maqelectricas
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 0:e6a49a092e2a | 1 | |
stephanie_liu | 38:3d32ce11e4d8 | 2 | //Importando bibliotecas |
Davidroid | 0:e6a49a092e2a | 3 | #include "mbed.h" |
Davidroid | 0:e6a49a092e2a | 4 | #include "DevSPI.h" |
davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 5 | #include "L6474.h" |
Davidroid | 0:e6a49a092e2a | 6 | |
stephanie_liu | 38:3d32ce11e4d8 | 7 | // Variaveis definição |
stephanie_liu | 38:3d32ce11e4d8 | 8 | #define STEPS_1 (200 * 2) /*NUMERO DE STEPs - 1 revolution given a 200 steps motor configured at 1/2 meio passo mode. */ |
Davidroid | 0:e6a49a092e2a | 9 | |
stephanie_liu | 38:3d32ce11e4d8 | 10 | // Delay in milliseconds |
Davidroid | 26:b0203c2265e5 | 11 | #define DELAY_1 1000 |
Davidroid | 26:b0203c2265e5 | 12 | #define DELAY_2 2000 |
Davidroid | 26:b0203c2265e5 | 13 | #define DELAY_3 6000 |
Davidroid | 26:b0203c2265e5 | 14 | #define DELAY_4 8000 |
Davidroid | 26:b0203c2265e5 | 15 | |
Davidroid | 29:a80a213c3c94 | 16 | /* Speed in pps (Pulses Per Second). |
Davidroid | 29:a80a213c3c94 | 17 | In Full Step mode: 1 pps = 1 step/s). |
Davidroid | 29:a80a213c3c94 | 18 | In 1/N Step Mode: N pps = 1 step/s). */ |
stephanie_liu | 38:3d32ce11e4d8 | 19 | #define SPEED_1 400 //passos por segundo V1 |
stephanie_liu | 38:3d32ce11e4d8 | 20 | |
stephanie_liu | 38:3d32ce11e4d8 | 21 | // Definindo botão |
stephanie_liu | 38:3d32ce11e4d8 | 22 | DigitalIn button(USER_BUTTON); |
stephanie_liu | 38:3d32ce11e4d8 | 23 | // inicialização da serial |
stephanie_liu | 38:3d32ce11e4d8 | 24 | Serial pc(USBTX, USBRX); |
Davidroid | 0:e6a49a092e2a | 25 | |
Davidroid | 0:e6a49a092e2a | 26 | |
Davidroid | 0:e6a49a092e2a | 27 | /* Variables -----------------------------------------------------------------*/ |
stephanie_liu | 38:3d32ce11e4d8 | 28 | int velocidade; |
Davidroid | 0:e6a49a092e2a | 29 | |
Davidroid | 19:1cd7f65c155c | 30 | /* Initialization parameters. */ |
davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 31 | L6474_init_t init = { |
stephanie_liu | 38:3d32ce11e4d8 | 32 | 900, /* Acceleration rate in pps^2. Range: (0..+inf). */ |
stephanie_liu | 38:3d32ce11e4d8 | 33 | 900, /* Deceleration rate in pps^2. Range: (0..+inf). */ |
stephanie_liu | 38:3d32ce11e4d8 | 34 | 1200, /* Maximum speed in pps. Range: (30..10000]. */ |
stephanie_liu | 38:3d32ce11e4d8 | 35 | 30, /* Minimum speed in pps. Range: [30..10000). */ |
Davidroid | 19:1cd7f65c155c | 36 | 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */ |
Davidroid | 19:1cd7f65c155c | 37 | L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */ |
Davidroid | 19:1cd7f65c155c | 38 | L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */ |
Davidroid | 19:1cd7f65c155c | 39 | L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */ |
stephanie_liu | 38:3d32ce11e4d8 | 40 | L6474_STEP_SEL_1_2, /* Step selection (STEP_SEL field of STEP_MODE register). */ //SETADO PARA MEIO PASSO |
Davidroid | 19:1cd7f65c155c | 41 | L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */ |
Davidroid | 19:1cd7f65c155c | 42 | L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */ |
Davidroid | 19:1cd7f65c155c | 43 | L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */ |
Davidroid | 19:1cd7f65c155c | 44 | 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */ |
Davidroid | 19:1cd7f65c155c | 45 | 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */ |
Davidroid | 19:1cd7f65c155c | 46 | L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */ |
Davidroid | 19:1cd7f65c155c | 47 | L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */ |
Davidroid | 19:1cd7f65c155c | 48 | L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */ |
Davidroid | 19:1cd7f65c155c | 49 | L6474_ALARM_EN_OVERCURRENT | |
Davidroid | 19:1cd7f65c155c | 50 | L6474_ALARM_EN_THERMAL_SHUTDOWN | |
Davidroid | 19:1cd7f65c155c | 51 | L6474_ALARM_EN_THERMAL_WARNING | |
Davidroid | 19:1cd7f65c155c | 52 | L6474_ALARM_EN_UNDERVOLTAGE | |
Davidroid | 19:1cd7f65c155c | 53 | L6474_ALARM_EN_SW_TURN_ON | |
Davidroid | 19:1cd7f65c155c | 54 | L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */ |
Davidroid | 19:1cd7f65c155c | 55 | }; |
Davidroid | 19:1cd7f65c155c | 56 | |
Davidroid | 0:e6a49a092e2a | 57 | /* Motor Control Component. */ |
Davidroid | 1:fbf28f3367aa | 58 | L6474 *motor; |
Davidroid | 0:e6a49a092e2a | 59 | |
Davidroid | 0:e6a49a092e2a | 60 | |
Davidroid | 19:1cd7f65c155c | 61 | /* Functions -----------------------------------------------------------------*/ |
Davidroid | 19:1cd7f65c155c | 62 | /** |
Davidroid | 25:3c863b420ac5 | 63 | * @brief This is an example of user handler for the flag interrupt. |
Davidroid | 25:3c863b420ac5 | 64 | * @param None |
Davidroid | 25:3c863b420ac5 | 65 | * @retval None |
Davidroid | 25:3c863b420ac5 | 66 | * @note If needed, implement it, and then attach and enable it: |
davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 67 | * + motor->attach_flag_irq(&flag_irq_handler); |
davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 68 | * + motor->enable_flag_irq(); |
Davidroid | 25:3c863b420ac5 | 69 | * To disable it: |
davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 70 | * + motor->disble_flag_irq(); |
Davidroid | 25:3c863b420ac5 | 71 | */ |
stephanie_liu | 38:3d32ce11e4d8 | 72 | void flag_irq_handler(void){ |
Davidroid | 19:1cd7f65c155c | 73 | /* Set ISR flag. */ |
Davidroid | 19:1cd7f65c155c | 74 | motor->isr_flag = TRUE; |
Davidroid | 19:1cd7f65c155c | 75 | |
Davidroid | 19:1cd7f65c155c | 76 | /* Get the value of the status register. */ |
davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 77 | unsigned int status = motor->get_status(); |
Davidroid | 34:543d0d1147d9 | 78 | |
Davidroid | 19:1cd7f65c155c | 79 | /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */ |
Davidroid | 19:1cd7f65c155c | 80 | /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */ |
Davidroid | 34:543d0d1147d9 | 81 | if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) { |
stephanie_liu | 38:3d32ce11e4d8 | 82 | printf("WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n"); |
Davidroid | 34:543d0d1147d9 | 83 | } |
stephanie_liu | 38:3d32ce11e4d8 | 84 | |
Davidroid | 19:1cd7f65c155c | 85 | /* Reset ISR flag. */ |
Davidroid | 19:1cd7f65c155c | 86 | motor->isr_flag = FALSE; |
Davidroid | 19:1cd7f65c155c | 87 | } |
Davidroid | 19:1cd7f65c155c | 88 | |
stephanie_liu | 38:3d32ce11e4d8 | 89 | int main(){ |
Davidroid | 6:32166bfc04b0 | 90 | /*----- Initialization. -----*/ |
stephanie_liu | 38:3d32ce11e4d8 | 91 | DevSPI dev_spi(D11, D12, D13); //Initializing SPI bus |
stephanie_liu | 38:3d32ce11e4d8 | 92 | pc.baud(9600); //Initializing serial |
Davidroid | 0:e6a49a092e2a | 93 | |
Davidroid | 11:3e303a25770d | 94 | /* Initializing Motor Control Component. */ |
Davidroid | 5:8065b587ade0 | 95 | motor = new L6474(D2, D8, D7, D9, D10, dev_spi); |
davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 96 | if (motor->init(&init) != COMPONENT_OK) { |
Davidroid | 17:4830b25fec7f | 97 | exit(EXIT_FAILURE); |
Davidroid | 34:543d0d1147d9 | 98 | } |
stephanie_liu | 38:3d32ce11e4d8 | 99 | |
Davidroid | 19:1cd7f65c155c | 100 | /* Attaching and enabling interrupt handlers. */ |
davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 101 | motor->attach_flag_irq(&flag_irq_handler); |
davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 102 | motor->enable_flag_irq(); |
Davidroid | 19:1cd7f65c155c | 103 | |
stephanie_liu | 38:3d32ce11e4d8 | 104 | while (1) { |
stephanie_liu | 38:3d32ce11e4d8 | 105 | velocidade = motor->get_speed(); // pegando a velocidade do motor |
stephanie_liu | 38:3d32ce11e4d8 | 106 | pc.printf("%d\r\n", velocidade); // print plotter serial da velocidade |
stephanie_liu | 38:3d32ce11e4d8 | 107 | if (button == 0) { |
stephanie_liu | 38:3d32ce11e4d8 | 108 | motor->set_max_speed(SPEED_1); /* Increasing the speed. */ |
stephanie_liu | 38:3d32ce11e4d8 | 109 | motor->move(StepperMotor::FWD, STEPS_1); /* realizando a rotação completa no sentido horario */ |
stephanie_liu | 38:3d32ce11e4d8 | 110 | motor->wait_while_active(); /* esperando o motor ser ativado. */ |
stephanie_liu | 38:3d32ce11e4d8 | 111 | wait(1); // Espera 1s antes de mudar de direç˜åo |
stephanie_liu | 38:3d32ce11e4d8 | 112 | motor->set_max_speed(SPEED_1); /* aumentando a velocidade até o valor de speed 1*/ |
stephanie_liu | 38:3d32ce11e4d8 | 113 | motor->move(StepperMotor::BWD, STEPS_1); /* SAH realizando a rotação completa*/ |
stephanie_liu | 38:3d32ce11e4d8 | 114 | } |
Davidroid | 0:e6a49a092e2a | 115 | } |
Davidroid | 0:e6a49a092e2a | 116 | } |