Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Lab_6_WaG by
stepper.cpp@49:80d4ffabec16, 2018-04-03 (annotated)
- Committer:
- spm71
- Date:
- Tue Apr 03 16:31:18 2018 +0000
- Revision:
- 49:80d4ffabec16
- Parent:
- 48:d612de6880b0
- Child:
- 50:e3a03bc1e1a6
Step motor code update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
spm71 | 18:0e281922212c | 1 | /****************************************************************************** |
spm71 | 18:0e281922212c | 2 | * EECS 397 |
spm71 | 18:0e281922212c | 3 | * |
spm71 | 42:6cba679a4ee4 | 4 | * Assignment Name: Lab 6: WaG |
spm71 | 18:0e281922212c | 5 | * |
spm71 | 18:0e281922212c | 6 | * Authors: Sam Morrison and Phong Nguyen |
spm71 | 18:0e281922212c | 7 | * File name: stepper.cpp |
spm71 | 18:0e281922212c | 8 | * Purpose: Driver for stepper motor |
spm71 | 18:0e281922212c | 9 | * |
spm71 | 18:0e281922212c | 10 | * Created: 03/02/2018 |
spm71 | 42:6cba679a4ee4 | 11 | * Last Modified: 03/29/2018 |
spm71 | 18:0e281922212c | 12 | * |
spm71 | 18:0e281922212c | 13 | ******************************************************************************/ |
spm71 | 18:0e281922212c | 14 | |
spm71 | 18:0e281922212c | 15 | #include "mbed.h" |
spm71 | 18:0e281922212c | 16 | #include "io_pins.h" |
spm71 | 18:0e281922212c | 17 | #include "spi.h" |
spm71 | 18:0e281922212c | 18 | #include "stepper.h" |
spm71 | 41:9b293b14b845 | 19 | #include "utility.h" |
spm71 | 49:80d4ffabec16 | 20 | #include "laser.h" |
spm71 | 49:80d4ffabec16 | 21 | #include "analog.h" |
spm71 | 49:80d4ffabec16 | 22 | #include "wag.h" |
spm71 | 18:0e281922212c | 23 | |
spm71 | 21:88f9f280931b | 24 | extern DigitalIn jog_ccw; |
spm71 | 21:88f9f280931b | 25 | extern DigitalIn jog_cw; |
spm71 | 35:ad2b3d6f0e5a | 26 | extern DigitalIn my_button; |
spm71 | 35:ad2b3d6f0e5a | 27 | extern DigitalIn cal_button; |
spm71 | 41:9b293b14b845 | 28 | extern DigitalIn home_sensor; |
spm71 | 22:09dd6977576b | 29 | extern Serial pc; |
spm71 | 49:80d4ffabec16 | 30 | extern DigitalOut laser; |
spm71 | 20:d23bcd97f2c5 | 31 | |
spm71 | 49:80d4ffabec16 | 32 | int stp_cur_pos = STP_POS_UNKN; |
phn10 | 44:4c2ba5bbba67 | 33 | int stp_sensor_pos[TGT_SENSOR_QUAN]; |
spm71 | 18:0e281922212c | 34 | |
spm71 | 18:0e281922212c | 35 | extern spi_cfg drv8806 { |
spm71 | 18:0e281922212c | 36 | SPI_DRV8806_ID, |
spm71 | 18:0e281922212c | 37 | STP_DRV8806_NCS, |
spm71 | 20:d23bcd97f2c5 | 38 | DRV8806_SPI_MODE, |
spm71 | 20:d23bcd97f2c5 | 39 | DRV8806_SPI_FREQ, |
spm71 | 20:d23bcd97f2c5 | 40 | DRV8806_SPI_NO_BITS, |
spm71 | 18:0e281922212c | 41 | }; |
spm71 | 18:0e281922212c | 42 | |
spm71 | 18:0e281922212c | 43 | /* |
spm71 | 18:0e281922212c | 44 | * void stp_init(); |
spm71 | 18:0e281922212c | 45 | * Description: initializes stepper values to unkown |
spm71 | 18:0e281922212c | 46 | * |
spm71 | 18:0e281922212c | 47 | * Inputs: |
spm71 | 18:0e281922212c | 48 | * Parameters: void |
spm71 | 18:0e281922212c | 49 | * Globals: |
spm71 | 18:0e281922212c | 50 | * |
spm71 | 18:0e281922212c | 51 | * Outputs: |
spm71 | 18:0e281922212c | 52 | * Returns: void |
spm71 | 18:0e281922212c | 53 | */ |
spm71 | 18:0e281922212c | 54 | void stp_init() { |
spm71 | 20:d23bcd97f2c5 | 55 | stp_cur_pos = STP_POS_UNKN; |
spm71 | 43:3b7f326aa096 | 56 | jog_cw.mode(PullUp); |
spm71 | 43:3b7f326aa096 | 57 | jog_ccw.mode(PullUp); |
spm71 | 43:3b7f326aa096 | 58 | cal_button.mode(PullUp); |
spm71 | 43:3b7f326aa096 | 59 | home_sensor.mode(PullUp); |
spm71 | 46:85c4b722baa7 | 60 | for (int i = 1; i <= TGT_SENSOR_QUAN; i++) { |
spm71 | 43:3b7f326aa096 | 61 | //stp_sensor_pos[i] = STP_POS_UNKN; |
spm71 | 18:0e281922212c | 62 | } |
spm71 | 18:0e281922212c | 63 | } |
spm71 | 18:0e281922212c | 64 | |
spm71 | 18:0e281922212c | 65 | /* |
spm71 | 18:0e281922212c | 66 | * void stp_step(int direction); |
spm71 | 18:0e281922212c | 67 | * Description: turns the stepper motor clockwise or counter-clockwise |
spm71 | 18:0e281922212c | 68 | * |
spm71 | 18:0e281922212c | 69 | * Inputs: |
spm71 | 18:0e281922212c | 70 | * Parameters: |
phn10 | 47:0547118e348e | 71 | * int direction: STP_CW for clock wise and STP_CCW for counter clock wise |
spm71 | 18:0e281922212c | 72 | * Globals: |
spm71 | 18:0e281922212c | 73 | * |
spm71 | 18:0e281922212c | 74 | * Outputs: |
spm71 | 18:0e281922212c | 75 | * Returns: void |
spm71 | 18:0e281922212c | 76 | */ |
spm71 | 18:0e281922212c | 77 | void stp_step(int direction) { |
spm71 | 20:d23bcd97f2c5 | 78 | |
spm71 | 20:d23bcd97f2c5 | 79 | //static int cur_pos = stp_cur_pos; |
spm71 | 20:d23bcd97f2c5 | 80 | static int turn[4] = {0x03, 0x06, 0x0c, 0x09}; |
spm71 | 20:d23bcd97f2c5 | 81 | if (direction == STP_CW) { |
spm71 | 49:80d4ffabec16 | 82 | |
spm71 | 49:80d4ffabec16 | 83 | if (stp_cur_pos <= 400) { |
spm71 | 49:80d4ffabec16 | 84 | if (stp_cur_pos != STP_POS_UNKN) |
spm71 | 49:80d4ffabec16 | 85 | stp_cur_pos++; |
spm71 | 49:80d4ffabec16 | 86 | } |
spm71 | 49:80d4ffabec16 | 87 | else { |
spm71 | 49:80d4ffabec16 | 88 | pc.printf("Cannot turn past maximum position. Fatal error.\n"); |
spm71 | 49:80d4ffabec16 | 89 | while(1); |
spm71 | 49:80d4ffabec16 | 90 | } |
spm71 | 49:80d4ffabec16 | 91 | |
spm71 | 49:80d4ffabec16 | 92 | |
spm71 | 22:09dd6977576b | 93 | for (int i = 0; i < 4; i++) { |
spm71 | 41:9b293b14b845 | 94 | wait(MOTOR_DELAY); |
spm71 | 22:09dd6977576b | 95 | //pc.printf("i = %d\n", i); |
spm71 | 20:d23bcd97f2c5 | 96 | spi_send(drv8806, turn[i]); |
spm71 | 22:09dd6977576b | 97 | } |
spm71 | 43:3b7f326aa096 | 98 | wait(0.015); |
spm71 | 20:d23bcd97f2c5 | 99 | } |
spm71 | 20:d23bcd97f2c5 | 100 | else if (direction == STP_CCW) { |
spm71 | 49:80d4ffabec16 | 101 | |
spm71 | 49:80d4ffabec16 | 102 | if (stp_cur_pos != 0) { |
spm71 | 49:80d4ffabec16 | 103 | if (stp_cur_pos != STP_POS_UNKN) |
spm71 | 49:80d4ffabec16 | 104 | stp_cur_pos--; |
spm71 | 49:80d4ffabec16 | 105 | } |
spm71 | 49:80d4ffabec16 | 106 | else { |
spm71 | 49:80d4ffabec16 | 107 | pc.printf("Cannot turn past home position.\n"); |
spm71 | 49:80d4ffabec16 | 108 | wait(0.5); |
spm71 | 49:80d4ffabec16 | 109 | return; |
spm71 | 49:80d4ffabec16 | 110 | } |
spm71 | 49:80d4ffabec16 | 111 | |
spm71 | 22:09dd6977576b | 112 | for (int i = 3; i >= 0; i--) { |
spm71 | 41:9b293b14b845 | 113 | wait(MOTOR_DELAY); |
spm71 | 22:09dd6977576b | 114 | //pc.printf("i = %d\n", i); |
spm71 | 20:d23bcd97f2c5 | 115 | spi_send(drv8806, turn[i]); |
spm71 | 22:09dd6977576b | 116 | } |
spm71 | 43:3b7f326aa096 | 117 | wait(0.015); |
spm71 | 20:d23bcd97f2c5 | 118 | } |
spm71 | 41:9b293b14b845 | 119 | wait(MOTOR_DELAY); |
spm71 | 35:ad2b3d6f0e5a | 120 | } |
spm71 | 35:ad2b3d6f0e5a | 121 | |
spm71 | 35:ad2b3d6f0e5a | 122 | /* |
spm71 | 35:ad2b3d6f0e5a | 123 | * void step_test(); |
spm71 | 35:ad2b3d6f0e5a | 124 | * Description: tests the stepper motor |
spm71 | 35:ad2b3d6f0e5a | 125 | * |
spm71 | 35:ad2b3d6f0e5a | 126 | * Inputs: |
spm71 | 35:ad2b3d6f0e5a | 127 | * Parameters: |
spm71 | 35:ad2b3d6f0e5a | 128 | * Globals: |
spm71 | 35:ad2b3d6f0e5a | 129 | * |
spm71 | 35:ad2b3d6f0e5a | 130 | * Outputs: |
spm71 | 35:ad2b3d6f0e5a | 131 | * Returns: void |
spm71 | 35:ad2b3d6f0e5a | 132 | */ |
spm71 | 35:ad2b3d6f0e5a | 133 | void step_test() { |
spm71 | 43:3b7f326aa096 | 134 | stp_init(); |
spm71 | 41:9b293b14b845 | 135 | while (uti_chk_ubutton() == 0); |
spm71 | 46:85c4b722baa7 | 136 | pc.printf("step motor test begin\n"); |
spm71 | 35:ad2b3d6f0e5a | 137 | while(1) { |
spm71 | 35:ad2b3d6f0e5a | 138 | if (jog_ccw == 0) { |
spm71 | 49:80d4ffabec16 | 139 | stp_step(STP_CCW); |
spm71 | 35:ad2b3d6f0e5a | 140 | } |
spm71 | 35:ad2b3d6f0e5a | 141 | if (jog_cw == 0) { |
spm71 | 49:80d4ffabec16 | 142 | stp_step(STP_CCW); |
spm71 | 35:ad2b3d6f0e5a | 143 | } |
spm71 | 35:ad2b3d6f0e5a | 144 | if (cal_button == 0) { |
spm71 | 35:ad2b3d6f0e5a | 145 | stp_find_home(); |
spm71 | 35:ad2b3d6f0e5a | 146 | } |
spm71 | 46:85c4b722baa7 | 147 | if (uti_chk_ubutton() == 1) |
spm71 | 46:85c4b722baa7 | 148 | break; |
spm71 | 35:ad2b3d6f0e5a | 149 | } |
spm71 | 35:ad2b3d6f0e5a | 150 | } |
spm71 | 35:ad2b3d6f0e5a | 151 | |
spm71 | 41:9b293b14b845 | 152 | /* |
spm71 | 41:9b293b14b845 | 153 | * void stp_find_home(); |
spm71 | 41:9b293b14b845 | 154 | * Description: uses the stepper motor and home sensor to find home |
spm71 | 41:9b293b14b845 | 155 | * |
spm71 | 41:9b293b14b845 | 156 | * Inputs: |
spm71 | 41:9b293b14b845 | 157 | * Parameters: |
spm71 | 41:9b293b14b845 | 158 | * Globals: |
spm71 | 41:9b293b14b845 | 159 | * |
spm71 | 41:9b293b14b845 | 160 | * Outputs: |
spm71 | 41:9b293b14b845 | 161 | * Returns: void |
spm71 | 41:9b293b14b845 | 162 | */ |
spm71 | 35:ad2b3d6f0e5a | 163 | void stp_find_home() { |
spm71 | 41:9b293b14b845 | 164 | int count = 0; |
spm71 | 41:9b293b14b845 | 165 | int half_count = 0; |
spm71 | 41:9b293b14b845 | 166 | stp_cur_pos = STP_POS_UNKN; |
spm71 | 41:9b293b14b845 | 167 | //pc.printf("Home sensor is currently %d\n", home_sensor.read()); |
spm71 | 41:9b293b14b845 | 168 | if (home_sensor == 0) { |
spm71 | 41:9b293b14b845 | 169 | for(int i = 0; i < 100; i++) |
spm71 | 41:9b293b14b845 | 170 | stp_step(STP_CW); |
spm71 | 41:9b293b14b845 | 171 | if (home_sensor == 0) { |
spm71 | 46:85c4b722baa7 | 172 | pc.printf("Error, home sensor not functioning. Fatal error.\n", home_sensor.read()); |
spm71 | 41:9b293b14b845 | 173 | while(1); |
spm71 | 41:9b293b14b845 | 174 | } |
spm71 | 41:9b293b14b845 | 175 | } |
spm71 | 41:9b293b14b845 | 176 | while (home_sensor.read() != 0) { |
spm71 | 41:9b293b14b845 | 177 | stp_step(STP_CCW); |
spm71 | 41:9b293b14b845 | 178 | } |
spm71 | 41:9b293b14b845 | 179 | while (home_sensor.read() != 1) { |
spm71 | 41:9b293b14b845 | 180 | stp_step(STP_CCW); |
spm71 | 41:9b293b14b845 | 181 | count++; |
spm71 | 41:9b293b14b845 | 182 | } |
spm71 | 41:9b293b14b845 | 183 | half_count = count/2; |
spm71 | 41:9b293b14b845 | 184 | for(int i = 0; i < half_count; i++) |
spm71 | 41:9b293b14b845 | 185 | stp_step(STP_CW); |
spm71 | 41:9b293b14b845 | 186 | stp_cur_pos = 0; |
spm71 | 41:9b293b14b845 | 187 | pc.printf("Home found.\n"); |
phn10 | 44:4c2ba5bbba67 | 188 | } |
phn10 | 44:4c2ba5bbba67 | 189 | |
phn10 | 44:4c2ba5bbba67 | 190 | |
phn10 | 44:4c2ba5bbba67 | 191 | /* |
phn10 | 44:4c2ba5bbba67 | 192 | * void stp_calibrate(int station, float * sensor_values, int * cal_status); |
phn10 | 44:4c2ba5bbba67 | 193 | * Description: uses the stepper motor and home sensor to find home |
phn10 | 44:4c2ba5bbba67 | 194 | * |
phn10 | 44:4c2ba5bbba67 | 195 | * Inputs: |
phn10 | 44:4c2ba5bbba67 | 196 | * Parameters: |
phn10 | 47:0547118e348e | 197 | * int station: STATION_A or STATION_B |
phn10 | 47:0547118e348e | 198 | * float *sensor_value: array of float holds 16 sensor values |
phn10 | 47:0547118e348e | 199 | * int *cal_status: pointer to int variable that will hold calibration status |
phn10 | 44:4c2ba5bbba67 | 200 | * Globals: |
phn10 | 44:4c2ba5bbba67 | 201 | * |
phn10 | 44:4c2ba5bbba67 | 202 | * Outputs: |
phn10 | 44:4c2ba5bbba67 | 203 | * Returns: void |
phn10 | 44:4c2ba5bbba67 | 204 | */ |
phn10 | 47:0547118e348e | 205 | void stp_calibrate(int station, float * sensor_values, int * cal_status){ |
spm71 | 49:80d4ffabec16 | 206 | while (uti_chk_ubutton() == 0); |
spm71 | 49:80d4ffabec16 | 207 | pc.printf("step 9 test begin\n"); |
spm71 | 49:80d4ffabec16 | 208 | |
phn10 | 47:0547118e348e | 209 | int sensor_no = 0; |
spm71 | 49:80d4ffabec16 | 210 | if (station == STATION_B) sensor_no = 8; |
phn10 | 47:0547118e348e | 211 | |
spm71 | 49:80d4ffabec16 | 212 | // find home position |
phn10 | 47:0547118e348e | 213 | stp_find_home(); |
phn10 | 47:0547118e348e | 214 | |
phn10 | 47:0547118e348e | 215 | // turn laser on |
spm71 | 49:80d4ffabec16 | 216 | wait(1); |
phn10 | 47:0547118e348e | 217 | lzr_on(); |
phn10 | 47:0547118e348e | 218 | |
phn10 | 47:0547118e348e | 219 | for (int i = 0; i < TGT_SENSOR_QUAN; i++) { |
phn10 | 47:0547118e348e | 220 | // scan all 16 sensors into sensor_values array |
phn10 | 47:0547118e348e | 221 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); |
phn10 | 47:0547118e348e | 222 | |
phn10 | 47:0547118e348e | 223 | // keep turning stepper motor clock wise until it points to a PT sensor |
phn10 | 47:0547118e348e | 224 | while (sensor_values[sensor_no + i] < PTTHRESH) { |
phn10 | 47:0547118e348e | 225 | // turn CW one step |
phn10 | 47:0547118e348e | 226 | stp_step(STP_CW); |
phn10 | 47:0547118e348e | 227 | |
phn10 | 47:0547118e348e | 228 | wait(0.005); |
phn10 | 47:0547118e348e | 229 | |
phn10 | 47:0547118e348e | 230 | // scan all PT sensors again |
phn10 | 47:0547118e348e | 231 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); |
phn10 | 47:0547118e348e | 232 | } |
phn10 | 47:0547118e348e | 233 | // found the sensor, save it's position |
phn10 | 47:0547118e348e | 234 | stp_sensor_pos[i] = stp_cur_pos; |
phn10 | 47:0547118e348e | 235 | } |
phn10 | 47:0547118e348e | 236 | // found position of all 8 sensors |
phn10 | 47:0547118e348e | 237 | |
phn10 | 47:0547118e348e | 238 | // go back home |
phn10 | 47:0547118e348e | 239 | stp_find_home(); |
phn10 | 47:0547118e348e | 240 | |
phn10 | 47:0547118e348e | 241 | *cal_status = CALIBRATED; |
phn10 | 47:0547118e348e | 242 | |
phn10 | 47:0547118e348e | 243 | // turn laser off |
phn10 | 47:0547118e348e | 244 | lzr_off(); |
phn10 | 47:0547118e348e | 245 | |
phn10 | 47:0547118e348e | 246 | // for debugging: print positions of all 8 sensors here |
phn10 | 47:0547118e348e | 247 | for (int i = 0; i < TGT_SENSOR_QUAN; i++) { |
phn10 | 47:0547118e348e | 248 | pc.printf("PT %d: %d ", i, stp_sensor_pos[i]); |
phn10 | 47:0547118e348e | 249 | } |
phn10 | 47:0547118e348e | 250 | } |