Software usado para produção de dois movimentos trapezoidais consecutivos.
Dependencies: mbed X_NUCLEO_IHM01A1
main_APS2_MEA.cpp@39:a932189cfaa6, 2021-10-27 (annotated)
- Committer:
- jonasl1
- Date:
- Wed Oct 27 02:53:07 2021 +0000
- Revision:
- 39:a932189cfaa6
- Parent:
- 38:3e9aca2ce6c5
Cod correto
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jonasl1 | 38:3e9aca2ce6c5 | 1 | /* Includes ------------------------------------------------------------------*/ |
jonasl1 | 38:3e9aca2ce6c5 | 2 | /* mbed specific header files. */ |
jonasl1 | 38:3e9aca2ce6c5 | 3 | #include "mbed.h" |
jonasl1 | 38:3e9aca2ce6c5 | 4 | |
jonasl1 | 38:3e9aca2ce6c5 | 5 | /* Helper header files. */ |
jonasl1 | 38:3e9aca2ce6c5 | 6 | #include "DevSPI.h" |
jonasl1 | 38:3e9aca2ce6c5 | 7 | |
jonasl1 | 38:3e9aca2ce6c5 | 8 | /* Component specific header files. */ |
jonasl1 | 38:3e9aca2ce6c5 | 9 | #include "L6474.h" |
jonasl1 | 38:3e9aca2ce6c5 | 10 | |
jonasl1 | 38:3e9aca2ce6c5 | 11 | |
jonasl1 | 38:3e9aca2ce6c5 | 12 | /* Definitions ---------------------------------------------------------------*/ |
jonasl1 | 38:3e9aca2ce6c5 | 13 | |
jonasl1 | 38:3e9aca2ce6c5 | 14 | InterruptIn user_b(USER_BUTTON); |
jonasl1 | 38:3e9aca2ce6c5 | 15 | |
jonasl1 | 38:3e9aca2ce6c5 | 16 | /* Number of steps. */ |
jonasl1 | 39:a932189cfaa6 | 17 | #define STEPS_1 800 /* 1 revolution given a 400 steps motor configured at 1/8 microstep mode. */ |
jonasl1 | 38:3e9aca2ce6c5 | 18 | |
jonasl1 | 38:3e9aca2ce6c5 | 19 | /* Delay in milliseconds. */ |
jonasl1 | 38:3e9aca2ce6c5 | 20 | #define DELAY_1 1000 |
jonasl1 | 38:3e9aca2ce6c5 | 21 | #define DELAY_2 2000 |
jonasl1 | 38:3e9aca2ce6c5 | 22 | #define DELAY_3 6000 |
jonasl1 | 38:3e9aca2ce6c5 | 23 | #define DELAY_4 8000 |
jonasl1 | 38:3e9aca2ce6c5 | 24 | |
jonasl1 | 38:3e9aca2ce6c5 | 25 | /* Speed in pps (Pulses Per Second). |
jonasl1 | 38:3e9aca2ce6c5 | 26 | In Full Step mode: 1 pps = 1 step/s). |
jonasl1 | 38:3e9aca2ce6c5 | 27 | In 1/N Step Mode: N pps = 1 step/s). */ |
jonasl1 | 38:3e9aca2ce6c5 | 28 | #define SPEED_1 2400 |
jonasl1 | 38:3e9aca2ce6c5 | 29 | #define SPEED_2 1200 |
jonasl1 | 38:3e9aca2ce6c5 | 30 | |
jonasl1 | 38:3e9aca2ce6c5 | 31 | void acionamento(void); //Prtótipo de função 1 |
jonasl1 | 38:3e9aca2ce6c5 | 32 | void acionamento1(void); //Protótipo de função 2 |
jonasl1 | 38:3e9aca2ce6c5 | 33 | bool i = 0; |
jonasl1 | 38:3e9aca2ce6c5 | 34 | |
jonasl1 | 38:3e9aca2ce6c5 | 35 | /* Variables -----------------------------------------------------------------*/ |
jonasl1 | 38:3e9aca2ce6c5 | 36 | |
jonasl1 | 38:3e9aca2ce6c5 | 37 | /* Initialization parameters. */ |
jonasl1 | 38:3e9aca2ce6c5 | 38 | L6474_init_t init = { |
jonasl1 | 38:3e9aca2ce6c5 | 39 | 1000, /* Acceleration rate in pps^2. Range: (0..+inf). */ |
jonasl1 | 38:3e9aca2ce6c5 | 40 | 1000, /* Deceleration rate in pps^2. Range: (0..+inf). */ |
jonasl1 | 39:a932189cfaa6 | 41 | 800, /* Maximum speed in pps. Range: (30..10000]. */ |
jonasl1 | 38:3e9aca2ce6c5 | 42 | 0, /* Minimum speed in pps. Range: [30..10000). */ |
jonasl1 | 38:3e9aca2ce6c5 | 43 | 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */ |
jonasl1 | 38:3e9aca2ce6c5 | 44 | L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */ |
jonasl1 | 38:3e9aca2ce6c5 | 45 | L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */ |
jonasl1 | 38:3e9aca2ce6c5 | 46 | L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */ |
jonasl1 | 38:3e9aca2ce6c5 | 47 | L6474_STEP_SEL_1_4, /* Step selection (STEP_SEL field of STEP_MODE register). */ |
jonasl1 | 38:3e9aca2ce6c5 | 48 | L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */ |
jonasl1 | 38:3e9aca2ce6c5 | 49 | L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */ |
jonasl1 | 38:3e9aca2ce6c5 | 50 | L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */ |
jonasl1 | 38:3e9aca2ce6c5 | 51 | 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */ |
jonasl1 | 38:3e9aca2ce6c5 | 52 | 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */ |
jonasl1 | 38:3e9aca2ce6c5 | 53 | L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */ |
jonasl1 | 38:3e9aca2ce6c5 | 54 | L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */ |
jonasl1 | 38:3e9aca2ce6c5 | 55 | L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */ |
jonasl1 | 38:3e9aca2ce6c5 | 56 | L6474_ALARM_EN_OVERCURRENT | |
jonasl1 | 38:3e9aca2ce6c5 | 57 | L6474_ALARM_EN_THERMAL_SHUTDOWN | |
jonasl1 | 38:3e9aca2ce6c5 | 58 | L6474_ALARM_EN_THERMAL_WARNING | |
jonasl1 | 38:3e9aca2ce6c5 | 59 | L6474_ALARM_EN_UNDERVOLTAGE | |
jonasl1 | 38:3e9aca2ce6c5 | 60 | L6474_ALARM_EN_SW_TURN_ON | |
jonasl1 | 38:3e9aca2ce6c5 | 61 | L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */ |
jonasl1 | 38:3e9aca2ce6c5 | 62 | }; |
jonasl1 | 38:3e9aca2ce6c5 | 63 | |
jonasl1 | 38:3e9aca2ce6c5 | 64 | /* Motor Control Component. */ |
jonasl1 | 38:3e9aca2ce6c5 | 65 | L6474 *motor; |
jonasl1 | 38:3e9aca2ce6c5 | 66 | |
jonasl1 | 38:3e9aca2ce6c5 | 67 | /* Functions -----------------------------------------------------------------*/ |
jonasl1 | 38:3e9aca2ce6c5 | 68 | |
jonasl1 | 38:3e9aca2ce6c5 | 69 | void flag_irq_handler(void) |
jonasl1 | 38:3e9aca2ce6c5 | 70 | { |
jonasl1 | 38:3e9aca2ce6c5 | 71 | /* Set ISR flag. */ |
jonasl1 | 38:3e9aca2ce6c5 | 72 | motor->isr_flag = TRUE; |
jonasl1 | 38:3e9aca2ce6c5 | 73 | |
jonasl1 | 38:3e9aca2ce6c5 | 74 | /* Get the value of the status register. */ |
jonasl1 | 38:3e9aca2ce6c5 | 75 | unsigned int status = motor->get_status(); |
jonasl1 | 38:3e9aca2ce6c5 | 76 | |
jonasl1 | 38:3e9aca2ce6c5 | 77 | /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */ |
jonasl1 | 38:3e9aca2ce6c5 | 78 | /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */ |
jonasl1 | 38:3e9aca2ce6c5 | 79 | if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) { |
jonasl1 | 38:3e9aca2ce6c5 | 80 | printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n"); |
jonasl1 | 38:3e9aca2ce6c5 | 81 | } |
jonasl1 | 38:3e9aca2ce6c5 | 82 | |
jonasl1 | 38:3e9aca2ce6c5 | 83 | /* Reset ISR flag. */ |
jonasl1 | 38:3e9aca2ce6c5 | 84 | motor->isr_flag = FALSE; |
jonasl1 | 38:3e9aca2ce6c5 | 85 | } |
jonasl1 | 38:3e9aca2ce6c5 | 86 | |
jonasl1 | 38:3e9aca2ce6c5 | 87 | |
jonasl1 | 38:3e9aca2ce6c5 | 88 | /* Main ----------------------------------------------------------------------*/ |
jonasl1 | 38:3e9aca2ce6c5 | 89 | |
jonasl1 | 38:3e9aca2ce6c5 | 90 | int main() |
jonasl1 | 38:3e9aca2ce6c5 | 91 | { |
jonasl1 | 38:3e9aca2ce6c5 | 92 | /* Initializing SPI bus. */ |
jonasl1 | 38:3e9aca2ce6c5 | 93 | DevSPI dev_spi(D11, D12, D13); |
jonasl1 | 38:3e9aca2ce6c5 | 94 | |
jonasl1 | 38:3e9aca2ce6c5 | 95 | /* Initializing Motor Control Component. */ |
jonasl1 | 38:3e9aca2ce6c5 | 96 | motor = new L6474(D2, D8, D7, D9, D10, dev_spi); |
jonasl1 | 38:3e9aca2ce6c5 | 97 | if (motor->init(&init) != COMPONENT_OK) { |
jonasl1 | 38:3e9aca2ce6c5 | 98 | exit(EXIT_FAILURE); |
jonasl1 | 38:3e9aca2ce6c5 | 99 | } |
jonasl1 | 38:3e9aca2ce6c5 | 100 | |
jonasl1 | 38:3e9aca2ce6c5 | 101 | /* Attaching and enabling interrupt handlers. */ |
jonasl1 | 38:3e9aca2ce6c5 | 102 | motor->attach_flag_irq(&flag_irq_handler); |
jonasl1 | 38:3e9aca2ce6c5 | 103 | motor->enable_flag_irq(); |
jonasl1 | 38:3e9aca2ce6c5 | 104 | |
jonasl1 | 38:3e9aca2ce6c5 | 105 | user_b.fall(&acionamento); |
jonasl1 | 38:3e9aca2ce6c5 | 106 | user_b.rise(&acionamento1); |
jonasl1 | 38:3e9aca2ce6c5 | 107 | |
jonasl1 | 38:3e9aca2ce6c5 | 108 | while(true) |
jonasl1 | 38:3e9aca2ce6c5 | 109 | { |
jonasl1 | 38:3e9aca2ce6c5 | 110 | if (i == 1) |
jonasl1 | 38:3e9aca2ce6c5 | 111 | { |
jonasl1 | 38:3e9aca2ce6c5 | 112 | /* Moving N steps in the forward direction. */ |
jonasl1 | 38:3e9aca2ce6c5 | 113 | motor->move(StepperMotor::FWD, 800); |
jonasl1 | 38:3e9aca2ce6c5 | 114 | /* Waiting while the motor is active. */ |
jonasl1 | 38:3e9aca2ce6c5 | 115 | motor->wait_while_active(); |
jonasl1 | 38:3e9aca2ce6c5 | 116 | wait_ms(500); //esperar meio segundo para executar o segundo movimento |
jonasl1 | 38:3e9aca2ce6c5 | 117 | |
jonasl1 | 38:3e9aca2ce6c5 | 118 | motor->move(StepperMotor::BWD, 800); |
jonasl1 | 38:3e9aca2ce6c5 | 119 | /* Waiting while the motor is active. */ |
jonasl1 | 38:3e9aca2ce6c5 | 120 | motor->wait_while_active(); |
jonasl1 | 38:3e9aca2ce6c5 | 121 | } |
jonasl1 | 38:3e9aca2ce6c5 | 122 | } |
jonasl1 | 38:3e9aca2ce6c5 | 123 | } |
jonasl1 | 38:3e9aca2ce6c5 | 124 | void acionamento() |
jonasl1 | 38:3e9aca2ce6c5 | 125 | { |
jonasl1 | 38:3e9aca2ce6c5 | 126 | i = 1; //Ao apertar do botão, I assume estado ligado |
jonasl1 | 38:3e9aca2ce6c5 | 127 | } |
jonasl1 | 38:3e9aca2ce6c5 | 128 | void acionamento1() |
jonasl1 | 38:3e9aca2ce6c5 | 129 | { |
jonasl1 | 38:3e9aca2ce6c5 | 130 | i = 0; //Ao botão voltar ao estado, I assume estado desligado |
jonasl1 | 38:3e9aca2ce6c5 | 131 | } |