Firmware for MC33926 evaluation on KL25Z-based EVB. This works with a Graphical User Interface (GUI) available on NXP.com to control a brushed DC motor using FRDM-33926ESEVM or FRDM-33926PNBEVM. The code enables control of the PWM frequency, duty cycle, enable/disable controls, invert, slew rate control, real-time current monitoring, and includes status flag pin monitoring for undervoltage, short circuit and over-temperature events.
Fork of Brushed_DC_Motor_Control_MC33926 by
main_copy.cpp@2:d98eb31a4b69, 2018-07-09 (annotated)
- Committer:
- nxf42866
- Date:
- Mon Jul 09 17:53:48 2018 +0000
- Revision:
- 2:d98eb31a4b69
New firmware for evaluation of MC33926 on KL25Z-based EVB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nxf42866 | 2:d98eb31a4b69 | 1 | #include "mbed.h" |
nxf42866 | 2:d98eb31a4b69 | 2 | #include "USBHID.h" |
nxf42866 | 2:d98eb31a4b69 | 3 | |
nxf42866 | 2:d98eb31a4b69 | 4 | // We declare a USBHID device. |
nxf42866 | 2:d98eb31a4b69 | 5 | // HID In/Out Reports are 64 Bytes long |
nxf42866 | 2:d98eb31a4b69 | 6 | // Vendor ID (VID): 0x15A2 |
nxf42866 | 2:d98eb31a4b69 | 7 | // Product ID (PID): 0x0138 |
nxf42866 | 2:d98eb31a4b69 | 8 | // Serial Number: 0x0001 |
nxf42866 | 2:d98eb31a4b69 | 9 | USBHID hid(64, 64, 0x15A2, 0x0138, 0x0001, true); |
nxf42866 | 2:d98eb31a4b69 | 10 | |
nxf42866 | 2:d98eb31a4b69 | 11 | //Setup Digital Outputs for the LEDs on the FRDM |
nxf42866 | 2:d98eb31a4b69 | 12 | //PwmOut red_led(LED1); |
nxf42866 | 2:d98eb31a4b69 | 13 | //DigitalOut green_led(LED2); |
nxf42866 | 2:d98eb31a4b69 | 14 | //DigitalOut blue_led(LED3); |
nxf42866 | 2:d98eb31a4b69 | 15 | |
nxf42866 | 2:d98eb31a4b69 | 16 | //Setup PWM and Digital Outputs from FRDM-KL25Z to FRDM-17510 |
nxf42866 | 2:d98eb31a4b69 | 17 | PwmOut IN1(PTA5); // Pin IN1 input to MC33926 (FRDM PIN Name) |
nxf42866 | 2:d98eb31a4b69 | 18 | PwmOut IN2(PTC8); // Pin IN2 input to MC33926 (FRDM PIN Name) |
nxf42866 | 2:d98eb31a4b69 | 19 | DigitalOut EN(PTE0); // Pin EN input to MC33926 (FRDM PIN Name) |
nxf42866 | 2:d98eb31a4b69 | 20 | DigitalOut DIS1(PTA2); // Pin D1 input to MC33926 (FRDM PIN Name) |
nxf42866 | 2:d98eb31a4b69 | 21 | DigitalOut D2B(PTD5); // Pin D2B input to MC33926 (FRDM PIN Name) |
nxf42866 | 2:d98eb31a4b69 | 22 | DigitalOut SLEW(PTA13); // Pin Slew input to MC33926 (FRDM PIN Name) |
nxf42866 | 2:d98eb31a4b69 | 23 | DigitalOut INV(PTD0); // Pin INV input to MC33926 (FRDM PIN Name) |
nxf42866 | 2:d98eb31a4b69 | 24 | DigitalIn SFB(PTB3); // Pin SF_B output from MC33926 to FRDM-KL25Z |
nxf42866 | 2:d98eb31a4b69 | 25 | AnalogIn CFB(PTB0); // Pin FB output from MC33926 to FRDM-KL25Z |
nxf42866 | 2:d98eb31a4b69 | 26 | //DigitalOut READY(PTC7); // Pin READY input to Motor Control Board (FRDM PIN Name) |
nxf42866 | 2:d98eb31a4b69 | 27 | |
nxf42866 | 2:d98eb31a4b69 | 28 | //Variables |
nxf42866 | 2:d98eb31a4b69 | 29 | int pwm_freq_lo; |
nxf42866 | 2:d98eb31a4b69 | 30 | int pwm_freq_hi; |
nxf42866 | 2:d98eb31a4b69 | 31 | int frequencyHz = 500; |
nxf42866 | 2:d98eb31a4b69 | 32 | int runstop = 0; |
nxf42866 | 2:d98eb31a4b69 | 33 | int direction = 1; |
nxf42866 | 2:d98eb31a4b69 | 34 | int braking; // needs to be initialized? |
nxf42866 | 2:d98eb31a4b69 | 35 | int dutycycle = 75; |
nxf42866 | 2:d98eb31a4b69 | 36 | int newDataFlag = 0; |
nxf42866 | 2:d98eb31a4b69 | 37 | int status = 0; |
nxf42866 | 2:d98eb31a4b69 | 38 | uint16_t CurrFB; |
nxf42866 | 2:d98eb31a4b69 | 39 | uint16_t CFBArray[101]; |
nxf42866 | 2:d98eb31a4b69 | 40 | uint32_t CFBTotal; |
nxf42866 | 2:d98eb31a4b69 | 41 | uint16_t CFBAvg; |
nxf42866 | 2:d98eb31a4b69 | 42 | uint16_t CFBtemp; |
nxf42866 | 2:d98eb31a4b69 | 43 | |
nxf42866 | 2:d98eb31a4b69 | 44 | //storage for send and receive data |
nxf42866 | 2:d98eb31a4b69 | 45 | HID_REPORT send_report; |
nxf42866 | 2:d98eb31a4b69 | 46 | HID_REPORT recv_report; |
nxf42866 | 2:d98eb31a4b69 | 47 | |
nxf42866 | 2:d98eb31a4b69 | 48 | bool initflag = true; |
nxf42866 | 2:d98eb31a4b69 | 49 | |
nxf42866 | 2:d98eb31a4b69 | 50 | // USB COMMANDS |
nxf42866 | 2:d98eb31a4b69 | 51 | // These are sent from the PC |
nxf42866 | 2:d98eb31a4b69 | 52 | #define WRITE_LED 0x20 |
nxf42866 | 2:d98eb31a4b69 | 53 | #define WRITE_GEN_EN 0x40 // what is this? What should the GUI kickoff here? |
nxf42866 | 2:d98eb31a4b69 | 54 | #define WRITE_DUTY_CYCLE 0x50 |
nxf42866 | 2:d98eb31a4b69 | 55 | #define WRITE_PWM_FREQ 0x60 |
nxf42866 | 2:d98eb31a4b69 | 56 | #define WRITE_RUN_STOP 0x70 |
nxf42866 | 2:d98eb31a4b69 | 57 | #define WRITE_DIRECTION 0x71 |
nxf42866 | 2:d98eb31a4b69 | 58 | #define WRITE_BRAKING 0x90 |
nxf42866 | 2:d98eb31a4b69 | 59 | #define WRITE_RESET 0xA0 // what is this? What should the GUI kickoff here? |
nxf42866 | 2:d98eb31a4b69 | 60 | #define WRITE_D1 0xB1 |
nxf42866 | 2:d98eb31a4b69 | 61 | #define WRITE_EN 0xC1 |
nxf42866 | 2:d98eb31a4b69 | 62 | #define WRITE_D2B 0xD5 |
nxf42866 | 2:d98eb31a4b69 | 63 | #define WRITE_SLEW 0xE5 |
nxf42866 | 2:d98eb31a4b69 | 64 | #define WRITE_INV 0xF5 |
nxf42866 | 2:d98eb31a4b69 | 65 | |
nxf42866 | 2:d98eb31a4b69 | 66 | #define scaleFactor 0.11868 |
nxf42866 | 2:d98eb31a4b69 | 67 | |
nxf42866 | 2:d98eb31a4b69 | 68 | // LOGICAL CONSTANTS |
nxf42866 | 2:d98eb31a4b69 | 69 | #define OFF 0x00 |
nxf42866 | 2:d98eb31a4b69 | 70 | #define ON 0x01 |
nxf42866 | 2:d98eb31a4b69 | 71 | |
nxf42866 | 2:d98eb31a4b69 | 72 | |
nxf42866 | 2:d98eb31a4b69 | 73 | int main() |
nxf42866 | 2:d98eb31a4b69 | 74 | { |
nxf42866 | 2:d98eb31a4b69 | 75 | send_report.length = 64; |
nxf42866 | 2:d98eb31a4b69 | 76 | recv_report.length = 64; |
nxf42866 | 2:d98eb31a4b69 | 77 | |
nxf42866 | 2:d98eb31a4b69 | 78 | |
nxf42866 | 2:d98eb31a4b69 | 79 | while(1) |
nxf42866 | 2:d98eb31a4b69 | 80 | { |
nxf42866 | 2:d98eb31a4b69 | 81 | //try to read a msg |
nxf42866 | 2:d98eb31a4b69 | 82 | if(hid.readNB(&recv_report)) |
nxf42866 | 2:d98eb31a4b69 | 83 | { |
nxf42866 | 2:d98eb31a4b69 | 84 | switch(recv_report.data[0]) //byte 0 of recv_report.data is command |
nxf42866 | 2:d98eb31a4b69 | 85 | { |
nxf42866 | 2:d98eb31a4b69 | 86 | //----------------------------------------------------------------------------------------------------------------- |
nxf42866 | 2:d98eb31a4b69 | 87 | // COMMAND PARSER |
nxf42866 | 2:d98eb31a4b69 | 88 | //----------------------------------------------------------------------------------------------------------------- |
nxf42866 | 2:d98eb31a4b69 | 89 | //////// |
nxf42866 | 2:d98eb31a4b69 | 90 | case WRITE_LED: |
nxf42866 | 2:d98eb31a4b69 | 91 | break; |
nxf42866 | 2:d98eb31a4b69 | 92 | //////// |
nxf42866 | 2:d98eb31a4b69 | 93 | |
nxf42866 | 2:d98eb31a4b69 | 94 | //////// |
nxf42866 | 2:d98eb31a4b69 | 95 | case WRITE_DUTY_CYCLE: |
nxf42866 | 2:d98eb31a4b69 | 96 | dutycycle = recv_report.data[1]; |
nxf42866 | 2:d98eb31a4b69 | 97 | newDataFlag = 1; |
nxf42866 | 2:d98eb31a4b69 | 98 | break; |
nxf42866 | 2:d98eb31a4b69 | 99 | //////// |
nxf42866 | 2:d98eb31a4b69 | 100 | case WRITE_PWM_FREQ: //PWM frequency can be larger than 1 byte |
nxf42866 | 2:d98eb31a4b69 | 101 | pwm_freq_lo = recv_report.data[1]; //so we have to re-assemble the number |
nxf42866 | 2:d98eb31a4b69 | 102 | pwm_freq_hi = recv_report.data[2] * 100; |
nxf42866 | 2:d98eb31a4b69 | 103 | frequencyHz = pwm_freq_lo + pwm_freq_hi; |
nxf42866 | 2:d98eb31a4b69 | 104 | newDataFlag = 1; |
nxf42866 | 2:d98eb31a4b69 | 105 | break; |
nxf42866 | 2:d98eb31a4b69 | 106 | //////// |
nxf42866 | 2:d98eb31a4b69 | 107 | case WRITE_RUN_STOP: |
nxf42866 | 2:d98eb31a4b69 | 108 | newDataFlag = 1; |
nxf42866 | 2:d98eb31a4b69 | 109 | if(recv_report.data[1] != 0) |
nxf42866 | 2:d98eb31a4b69 | 110 | { |
nxf42866 | 2:d98eb31a4b69 | 111 | runstop = 1; |
nxf42866 | 2:d98eb31a4b69 | 112 | } |
nxf42866 | 2:d98eb31a4b69 | 113 | else |
nxf42866 | 2:d98eb31a4b69 | 114 | { |
nxf42866 | 2:d98eb31a4b69 | 115 | runstop = 0; |
nxf42866 | 2:d98eb31a4b69 | 116 | } |
nxf42866 | 2:d98eb31a4b69 | 117 | break; |
nxf42866 | 2:d98eb31a4b69 | 118 | |
nxf42866 | 2:d98eb31a4b69 | 119 | //////// |
nxf42866 | 2:d98eb31a4b69 | 120 | case WRITE_DIRECTION: |
nxf42866 | 2:d98eb31a4b69 | 121 | newDataFlag = 1; |
nxf42866 | 2:d98eb31a4b69 | 122 | if(recv_report.data[1] == 1) // used to be != 0 |
nxf42866 | 2:d98eb31a4b69 | 123 | { |
nxf42866 | 2:d98eb31a4b69 | 124 | direction = 1; // corrected allocation for FWD |
nxf42866 | 2:d98eb31a4b69 | 125 | //direction = 0; // corrected allocation for REV |
nxf42866 | 2:d98eb31a4b69 | 126 | } |
nxf42866 | 2:d98eb31a4b69 | 127 | else |
nxf42866 | 2:d98eb31a4b69 | 128 | { |
nxf42866 | 2:d98eb31a4b69 | 129 | direction = 0; // corrected allocation for REV |
nxf42866 | 2:d98eb31a4b69 | 130 | //direction = 1; // corrected allocation for FWD |
nxf42866 | 2:d98eb31a4b69 | 131 | } |
nxf42866 | 2:d98eb31a4b69 | 132 | break; |
nxf42866 | 2:d98eb31a4b69 | 133 | //////// |
nxf42866 | 2:d98eb31a4b69 | 134 | case WRITE_BRAKING: |
nxf42866 | 2:d98eb31a4b69 | 135 | newDataFlag = 1; |
nxf42866 | 2:d98eb31a4b69 | 136 | if(recv_report.data[1] != 0) // used to be == 1 |
nxf42866 | 2:d98eb31a4b69 | 137 | { |
nxf42866 | 2:d98eb31a4b69 | 138 | braking = 1; // this is HS recirc |
nxf42866 | 2:d98eb31a4b69 | 139 | } |
nxf42866 | 2:d98eb31a4b69 | 140 | else |
nxf42866 | 2:d98eb31a4b69 | 141 | { |
nxf42866 | 2:d98eb31a4b69 | 142 | braking = 0; // this is LS recirc |
nxf42866 | 2:d98eb31a4b69 | 143 | } |
nxf42866 | 2:d98eb31a4b69 | 144 | break; |
nxf42866 | 2:d98eb31a4b69 | 145 | //////// |
nxf42866 | 2:d98eb31a4b69 | 146 | case WRITE_D1: |
nxf42866 | 2:d98eb31a4b69 | 147 | newDataFlag = 1; |
nxf42866 | 2:d98eb31a4b69 | 148 | if(recv_report.data[1] == 1) |
nxf42866 | 2:d98eb31a4b69 | 149 | { |
nxf42866 | 2:d98eb31a4b69 | 150 | DIS1 = 1; // logic hi will disable the part |
nxf42866 | 2:d98eb31a4b69 | 151 | } |
nxf42866 | 2:d98eb31a4b69 | 152 | else |
nxf42866 | 2:d98eb31a4b69 | 153 | { |
nxf42866 | 2:d98eb31a4b69 | 154 | DIS1 = 0; // logic lo will enable the part |
nxf42866 | 2:d98eb31a4b69 | 155 | } |
nxf42866 | 2:d98eb31a4b69 | 156 | break; |
nxf42866 | 2:d98eb31a4b69 | 157 | //////// |
nxf42866 | 2:d98eb31a4b69 | 158 | case WRITE_EN: |
nxf42866 | 2:d98eb31a4b69 | 159 | newDataFlag = 1; |
nxf42866 | 2:d98eb31a4b69 | 160 | if(recv_report.data[1] == 1) |
nxf42866 | 2:d98eb31a4b69 | 161 | { |
nxf42866 | 2:d98eb31a4b69 | 162 | //EN = 0; // this is enable case |
nxf42866 | 2:d98eb31a4b69 | 163 | EN = 1; // align with the signal being sent from GUI |
nxf42866 | 2:d98eb31a4b69 | 164 | } |
nxf42866 | 2:d98eb31a4b69 | 165 | else |
nxf42866 | 2:d98eb31a4b69 | 166 | { |
nxf42866 | 2:d98eb31a4b69 | 167 | //EN = 1; // this is disable case |
nxf42866 | 2:d98eb31a4b69 | 168 | EN = 0; // align with signal being sent from GUI |
nxf42866 | 2:d98eb31a4b69 | 169 | } |
nxf42866 | 2:d98eb31a4b69 | 170 | break; |
nxf42866 | 2:d98eb31a4b69 | 171 | //////// |
nxf42866 | 2:d98eb31a4b69 | 172 | case WRITE_D2B: |
nxf42866 | 2:d98eb31a4b69 | 173 | newDataFlag = 1; |
nxf42866 | 2:d98eb31a4b69 | 174 | if(recv_report.data[1] == 1) |
nxf42866 | 2:d98eb31a4b69 | 175 | { |
nxf42866 | 2:d98eb31a4b69 | 176 | D2B = 1; |
nxf42866 | 2:d98eb31a4b69 | 177 | } |
nxf42866 | 2:d98eb31a4b69 | 178 | else |
nxf42866 | 2:d98eb31a4b69 | 179 | { |
nxf42866 | 2:d98eb31a4b69 | 180 | D2B = 0; |
nxf42866 | 2:d98eb31a4b69 | 181 | } |
nxf42866 | 2:d98eb31a4b69 | 182 | break; |
nxf42866 | 2:d98eb31a4b69 | 183 | |
nxf42866 | 2:d98eb31a4b69 | 184 | //////// |
nxf42866 | 2:d98eb31a4b69 | 185 | case WRITE_SLEW: |
nxf42866 | 2:d98eb31a4b69 | 186 | newDataFlag = 1; |
nxf42866 | 2:d98eb31a4b69 | 187 | if(recv_report.data[1] == 1) |
nxf42866 | 2:d98eb31a4b69 | 188 | { |
nxf42866 | 2:d98eb31a4b69 | 189 | SLEW = 1; |
nxf42866 | 2:d98eb31a4b69 | 190 | } |
nxf42866 | 2:d98eb31a4b69 | 191 | else |
nxf42866 | 2:d98eb31a4b69 | 192 | { |
nxf42866 | 2:d98eb31a4b69 | 193 | SLEW = 0; |
nxf42866 | 2:d98eb31a4b69 | 194 | } |
nxf42866 | 2:d98eb31a4b69 | 195 | break; |
nxf42866 | 2:d98eb31a4b69 | 196 | |
nxf42866 | 2:d98eb31a4b69 | 197 | //////// |
nxf42866 | 2:d98eb31a4b69 | 198 | case WRITE_INV: |
nxf42866 | 2:d98eb31a4b69 | 199 | newDataFlag = 1; |
nxf42866 | 2:d98eb31a4b69 | 200 | if(recv_report.data[1] == 1) |
nxf42866 | 2:d98eb31a4b69 | 201 | { |
nxf42866 | 2:d98eb31a4b69 | 202 | INV = 1; |
nxf42866 | 2:d98eb31a4b69 | 203 | //INV = 0; |
nxf42866 | 2:d98eb31a4b69 | 204 | } |
nxf42866 | 2:d98eb31a4b69 | 205 | else |
nxf42866 | 2:d98eb31a4b69 | 206 | { |
nxf42866 | 2:d98eb31a4b69 | 207 | INV = 0; |
nxf42866 | 2:d98eb31a4b69 | 208 | //INV = 1; |
nxf42866 | 2:d98eb31a4b69 | 209 | } |
nxf42866 | 2:d98eb31a4b69 | 210 | break; |
nxf42866 | 2:d98eb31a4b69 | 211 | |
nxf42866 | 2:d98eb31a4b69 | 212 | |
nxf42866 | 2:d98eb31a4b69 | 213 | //////// |
nxf42866 | 2:d98eb31a4b69 | 214 | default: |
nxf42866 | 2:d98eb31a4b69 | 215 | break; |
nxf42866 | 2:d98eb31a4b69 | 216 | }// End Switch recv report data[0] |
nxf42866 | 2:d98eb31a4b69 | 217 | |
nxf42866 | 2:d98eb31a4b69 | 218 | //----------------------------------------------------------------------------------------------------------------- |
nxf42866 | 2:d98eb31a4b69 | 219 | // end command parser |
nxf42866 | 2:d98eb31a4b69 | 220 | //----------------------------------------------------------------------------------------------------------------- |
nxf42866 | 2:d98eb31a4b69 | 221 | |
nxf42866 | 2:d98eb31a4b69 | 222 | status = SFB; |
nxf42866 | 2:d98eb31a4b69 | 223 | send_report.data[0] = status; // Echo Command |
nxf42866 | 2:d98eb31a4b69 | 224 | send_report.data[1] = recv_report.data[1]; // Echo Subcommand 1 |
nxf42866 | 2:d98eb31a4b69 | 225 | send_report.data[2] = recv_report.data[2]; // Echo Subcommand 2 |
nxf42866 | 2:d98eb31a4b69 | 226 | send_report.data[3] = 0x00; |
nxf42866 | 2:d98eb31a4b69 | 227 | send_report.data[4] = 0x00; |
nxf42866 | 2:d98eb31a4b69 | 228 | send_report.data[5] = 0x00; |
nxf42866 | 2:d98eb31a4b69 | 229 | send_report.data[6] = (CFBAvg << 8) >> 8; |
nxf42866 | 2:d98eb31a4b69 | 230 | send_report.data[7] = CFBAvg >> 8; |
nxf42866 | 2:d98eb31a4b69 | 231 | |
nxf42866 | 2:d98eb31a4b69 | 232 | //Send the report |
nxf42866 | 2:d98eb31a4b69 | 233 | hid.send(&send_report); |
nxf42866 | 2:d98eb31a4b69 | 234 | }// End If(hid.readNB(&recv_report)) |
nxf42866 | 2:d98eb31a4b69 | 235 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
nxf42866 | 2:d98eb31a4b69 | 236 | //End of USB message handling |
nxf42866 | 2:d98eb31a4b69 | 237 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
nxf42866 | 2:d98eb31a4b69 | 238 | |
nxf42866 | 2:d98eb31a4b69 | 239 | CurrFB = CFB.read_u16(); |
nxf42866 | 2:d98eb31a4b69 | 240 | CFBArray[0] = CurrFB; |
nxf42866 | 2:d98eb31a4b69 | 241 | CFBTotal = 0; |
nxf42866 | 2:d98eb31a4b69 | 242 | int i = 0; |
nxf42866 | 2:d98eb31a4b69 | 243 | for(i=0; i<100; i++) |
nxf42866 | 2:d98eb31a4b69 | 244 | { |
nxf42866 | 2:d98eb31a4b69 | 245 | CFBTotal = CFBTotal + CFBArray[i]; |
nxf42866 | 2:d98eb31a4b69 | 246 | } |
nxf42866 | 2:d98eb31a4b69 | 247 | CFBAvg = CFBTotal / 100; |
nxf42866 | 2:d98eb31a4b69 | 248 | |
nxf42866 | 2:d98eb31a4b69 | 249 | for(i=100; i>=0; i--) |
nxf42866 | 2:d98eb31a4b69 | 250 | { |
nxf42866 | 2:d98eb31a4b69 | 251 | |
nxf42866 | 2:d98eb31a4b69 | 252 | CFBArray[i+1] = CFBArray[i]; |
nxf42866 | 2:d98eb31a4b69 | 253 | } |
nxf42866 | 2:d98eb31a4b69 | 254 | |
nxf42866 | 2:d98eb31a4b69 | 255 | if(newDataFlag != 0) //GUI setting changed |
nxf42866 | 2:d98eb31a4b69 | 256 | { |
nxf42866 | 2:d98eb31a4b69 | 257 | newDataFlag = 0; |
nxf42866 | 2:d98eb31a4b69 | 258 | if(runstop != 0) //Running |
nxf42866 | 2:d98eb31a4b69 | 259 | { |
nxf42866 | 2:d98eb31a4b69 | 260 | if(direction == 0) //reverse |
nxf42866 | 2:d98eb31a4b69 | 261 | { |
nxf42866 | 2:d98eb31a4b69 | 262 | if(braking == 1) //dynamic |
nxf42866 | 2:d98eb31a4b69 | 263 | { |
nxf42866 | 2:d98eb31a4b69 | 264 | IN1.period(1/(float)frequencyHz); |
nxf42866 | 2:d98eb31a4b69 | 265 | //IN1 = (float)dutycycle/100.0; // this is REV + HS, inverted d |
nxf42866 | 2:d98eb31a4b69 | 266 | IN1 = 1.0-((float)dutycycle/100.0); // this is REV + HS |
nxf42866 | 2:d98eb31a4b69 | 267 | IN2 = 1; |
nxf42866 | 2:d98eb31a4b69 | 268 | } |
nxf42866 | 2:d98eb31a4b69 | 269 | else //coast |
nxf42866 | 2:d98eb31a4b69 | 270 | { |
nxf42866 | 2:d98eb31a4b69 | 271 | IN1 = 0; // this is REV + LS |
nxf42866 | 2:d98eb31a4b69 | 272 | IN2.period(1/(float)frequencyHz); |
nxf42866 | 2:d98eb31a4b69 | 273 | IN2 = (float)dutycycle/100.0; |
nxf42866 | 2:d98eb31a4b69 | 274 | } |
nxf42866 | 2:d98eb31a4b69 | 275 | } |
nxf42866 | 2:d98eb31a4b69 | 276 | else //forward |
nxf42866 | 2:d98eb31a4b69 | 277 | { |
nxf42866 | 2:d98eb31a4b69 | 278 | if(braking == 1) //dynamic |
nxf42866 | 2:d98eb31a4b69 | 279 | { |
nxf42866 | 2:d98eb31a4b69 | 280 | IN1 = 1; |
nxf42866 | 2:d98eb31a4b69 | 281 | IN2.period(1/(float)frequencyHz); |
nxf42866 | 2:d98eb31a4b69 | 282 | //IN2 = (float)dutycycle/100.0; // this is FWD + HS, inverted d |
nxf42866 | 2:d98eb31a4b69 | 283 | IN2 = 1.0-((float)dutycycle/100.0); // this is FWD + HS |
nxf42866 | 2:d98eb31a4b69 | 284 | } |
nxf42866 | 2:d98eb31a4b69 | 285 | else //coast |
nxf42866 | 2:d98eb31a4b69 | 286 | { |
nxf42866 | 2:d98eb31a4b69 | 287 | IN1.period(1/(float)frequencyHz); |
nxf42866 | 2:d98eb31a4b69 | 288 | IN1 = (float)dutycycle/100.0; |
nxf42866 | 2:d98eb31a4b69 | 289 | IN2 = 0; // FWD + LS |
nxf42866 | 2:d98eb31a4b69 | 290 | } |
nxf42866 | 2:d98eb31a4b69 | 291 | } |
nxf42866 | 2:d98eb31a4b69 | 292 | } |
nxf42866 | 2:d98eb31a4b69 | 293 | else //Stopped |
nxf42866 | 2:d98eb31a4b69 | 294 | { |
nxf42866 | 2:d98eb31a4b69 | 295 | if(braking == 1) //braking |
nxf42866 | 2:d98eb31a4b69 | 296 | { |
nxf42866 | 2:d98eb31a4b69 | 297 | IN1.period(1); |
nxf42866 | 2:d98eb31a4b69 | 298 | IN2.period(1); |
nxf42866 | 2:d98eb31a4b69 | 299 | IN1.write(1); |
nxf42866 | 2:d98eb31a4b69 | 300 | IN2.write(1); |
nxf42866 | 2:d98eb31a4b69 | 301 | } |
nxf42866 | 2:d98eb31a4b69 | 302 | else //coasting |
nxf42866 | 2:d98eb31a4b69 | 303 | { |
nxf42866 | 2:d98eb31a4b69 | 304 | IN1.period(1); |
nxf42866 | 2:d98eb31a4b69 | 305 | IN2.period(1); |
nxf42866 | 2:d98eb31a4b69 | 306 | IN1.write(0); |
nxf42866 | 2:d98eb31a4b69 | 307 | IN2.write(0); |
nxf42866 | 2:d98eb31a4b69 | 308 | |
nxf42866 | 2:d98eb31a4b69 | 309 | } |
nxf42866 | 2:d98eb31a4b69 | 310 | } |
nxf42866 | 2:d98eb31a4b69 | 311 | } |
nxf42866 | 2:d98eb31a4b69 | 312 | } |
nxf42866 | 2:d98eb31a4b69 | 313 | } |
nxf42866 | 2:d98eb31a4b69 | 314 |