aaaaaaaaa

Dependencies:   mbed X_NUCLEO_IHM01A1

Committer:
zezo
Date:
Sat Oct 23 22:37:30 2021 +0000
Revision:
38:8e713e8246ef
Parent:
35:2b44ed4ec7a0
aaaaaaaa;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:e6a49a092e2a 1 /* Includes ------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 2
Davidroid 0:e6a49a092e2a 3 /* mbed specific header files. */
Davidroid 0:e6a49a092e2a 4 #include "mbed.h"
Davidroid 0:e6a49a092e2a 5
Davidroid 0:e6a49a092e2a 6 /* Helper header files. */
Davidroid 0:e6a49a092e2a 7 #include "DevSPI.h"
Davidroid 0:e6a49a092e2a 8
Davidroid 0:e6a49a092e2a 9 /* Component specific header files. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 10 #include "L6474.h"
Davidroid 0:e6a49a092e2a 11
Davidroid 0:e6a49a092e2a 12 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 13
Davidroid 26:b0203c2265e5 14 /* Number of steps. */
zezo 38:8e713e8246ef 15 #define STEPS_1 (200 * 2) /* 1 revolution given a 400 steps motor configured at 1/2 microstep mode. */
Davidroid 26:b0203c2265e5 16
Davidroid 29:a80a213c3c94 17 /* Speed in pps (Pulses Per Second).
Davidroid 29:a80a213c3c94 18 In Full Step mode: 1 pps = 1 step/s).
Davidroid 29:a80a213c3c94 19 In 1/N Step Mode: N pps = 1 step/s). */
zezo 38:8e713e8246ef 20 #define SPEED_1 500 //pps
Davidroid 0:e6a49a092e2a 21
Davidroid 0:e6a49a092e2a 22 /* Variables -----------------------------------------------------------------*/
zezo 38:8e713e8246ef 23 DigitalIn botao(USER_BUTTON); //Declarando o botão de usuário
zezo 38:8e713e8246ef 24 int speed;
Davidroid 0:e6a49a092e2a 25
Davidroid 19:1cd7f65c155c 26 /* Initialization parameters. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 27 L6474_init_t init = {
zezo 38:8e713e8246ef 28 1000, /* Acceleration rate in pps^2. Range: (0..+inf). */
zezo 38:8e713e8246ef 29 1000, /* Deceleration rate in pps^2. Range: (0..+inf). */
zezo 38:8e713e8246ef 30 1200, /* Maximum speed in pps. Range: (30..10000]. */
zezo 38:8e713e8246ef 31 30, /* Minimum speed in pps. Range: [30..10000). */
Davidroid 19:1cd7f65c155c 32 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
Davidroid 19:1cd7f65c155c 33 L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */
Davidroid 19:1cd7f65c155c 34 L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
Davidroid 19:1cd7f65c155c 35 L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */
zezo 38:8e713e8246ef 36 L6474_STEP_SEL_1_2, /* Step selection (STEP_SEL field of STEP_MODE register). */
Davidroid 19:1cd7f65c155c 37 L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */
Davidroid 19:1cd7f65c155c 38 L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
Davidroid 19:1cd7f65c155c 39 L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
Davidroid 19:1cd7f65c155c 40 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
Davidroid 19:1cd7f65c155c 41 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
Davidroid 19:1cd7f65c155c 42 L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */
Davidroid 19:1cd7f65c155c 43 L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */
Davidroid 19:1cd7f65c155c 44 L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
Davidroid 19:1cd7f65c155c 45 L6474_ALARM_EN_OVERCURRENT |
Davidroid 19:1cd7f65c155c 46 L6474_ALARM_EN_THERMAL_SHUTDOWN |
Davidroid 19:1cd7f65c155c 47 L6474_ALARM_EN_THERMAL_WARNING |
Davidroid 19:1cd7f65c155c 48 L6474_ALARM_EN_UNDERVOLTAGE |
Davidroid 19:1cd7f65c155c 49 L6474_ALARM_EN_SW_TURN_ON |
Davidroid 19:1cd7f65c155c 50 L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */
Davidroid 19:1cd7f65c155c 51 };
Davidroid 19:1cd7f65c155c 52
Davidroid 0:e6a49a092e2a 53 /* Motor Control Component. */
Davidroid 1:fbf28f3367aa 54 L6474 *motor;
Davidroid 0:e6a49a092e2a 55
Davidroid 19:1cd7f65c155c 56 /* Functions -----------------------------------------------------------------*/
Davidroid 19:1cd7f65c155c 57
davide.aliprandi@st.com 35:2b44ed4ec7a0 58 void flag_irq_handler(void)
Davidroid 19:1cd7f65c155c 59 {
Davidroid 19:1cd7f65c155c 60 /* Set ISR flag. */
Davidroid 19:1cd7f65c155c 61 motor->isr_flag = TRUE;
Davidroid 19:1cd7f65c155c 62
Davidroid 19:1cd7f65c155c 63 /* Get the value of the status register. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 64 unsigned int status = motor->get_status();
Davidroid 34:543d0d1147d9 65
Davidroid 19:1cd7f65c155c 66 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
Davidroid 19:1cd7f65c155c 67 /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */
Davidroid 34:543d0d1147d9 68 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) {
Davidroid 19:1cd7f65c155c 69 printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n");
Davidroid 34:543d0d1147d9 70 }
zezo 38:8e713e8246ef 71
Davidroid 19:1cd7f65c155c 72 /* Reset ISR flag. */
Davidroid 19:1cd7f65c155c 73 motor->isr_flag = FALSE;
Davidroid 19:1cd7f65c155c 74 }
Davidroid 19:1cd7f65c155c 75
Davidroid 0:e6a49a092e2a 76 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 77 int main()
Davidroid 0:e6a49a092e2a 78 {
Davidroid 6:32166bfc04b0 79 /*----- Initialization. -----*/
Davidroid 6:32166bfc04b0 80
Davidroid 0:e6a49a092e2a 81 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 82 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 83
Davidroid 11:3e303a25770d 84 /* Initializing Motor Control Component. */
Davidroid 5:8065b587ade0 85 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
davide.aliprandi@st.com 35:2b44ed4ec7a0 86 if (motor->init(&init) != COMPONENT_OK) {
Davidroid 17:4830b25fec7f 87 exit(EXIT_FAILURE);
Davidroid 34:543d0d1147d9 88 }
Davidroid 0:e6a49a092e2a 89
Davidroid 19:1cd7f65c155c 90 /* Attaching and enabling interrupt handlers. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 91 motor->attach_flag_irq(&flag_irq_handler);
davide.aliprandi@st.com 35:2b44ed4ec7a0 92 motor->enable_flag_irq();
Davidroid 19:1cd7f65c155c 93
Davidroid 6:32166bfc04b0 94
Davidroid 6:32166bfc04b0 95
zezo 38:8e713e8246ef 96 while (1) {
zezo 38:8e713e8246ef 97
zezo 38:8e713e8246ef 98 /* Getting motor speed */
zezo 38:8e713e8246ef 99 speed = motor->get_speed();
Davidroid 6:32166bfc04b0 100
zezo 38:8e713e8246ef 101 /* Printing speed to the console. */
zezo 38:8e713e8246ef 102 printf("%d\r\n", speed);
Davidroid 6:32166bfc04b0 103
zezo 38:8e713e8246ef 104 if (botao == 0) {
zezo 38:8e713e8246ef 105 /* Increasing the speed. */
zezo 38:8e713e8246ef 106 motor->set_max_speed(SPEED_1);
Davidroid 6:32166bfc04b0 107
zezo 38:8e713e8246ef 108 /* Moving 400 half steps in the forward direction. */
zezo 38:8e713e8246ef 109 motor->move(StepperMotor::FWD, STEPS_1);
Davidroid 0:e6a49a092e2a 110
zezo 38:8e713e8246ef 111 /* Waiting while the motor is active. */
zezo 38:8e713e8246ef 112 motor->wait_while_active();
Davidroid 6:32166bfc04b0 113
zezo 38:8e713e8246ef 114 wait(1); //Espera 1s antes de mudar de direç˜åo
Davidroid 6:32166bfc04b0 115
zezo 38:8e713e8246ef 116 /* Increasing the speed. */
zezo 38:8e713e8246ef 117 motor->set_max_speed(SPEED_1);
Davidroid 6:32166bfc04b0 118
zezo 38:8e713e8246ef 119 /* Moving 400 half steps in the backward direction. */
zezo 38:8e713e8246ef 120 motor->move(StepperMotor::BWD, STEPS_1);
zezo 38:8e713e8246ef 121 }
Davidroid 0:e6a49a092e2a 122 }
Davidroid 0:e6a49a092e2a 123 }