Lab 6 code.

Dependencies:   mbed

Fork of WaG by GroupA

Committer:
spm71
Date:
Fri Apr 06 18:25:19 2018 +0000
Revision:
56:048b30c9f2a1
Parent:
55:1a25dd75e309
Final update

Who changed what in which revision?

UserRevisionLine numberNew 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 56:048b30c9f2a1 11 * Last Modified: 04/06/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 36:ad2b3d6f0e5a 26 extern DigitalIn my_button;
spm71 36: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 52:8987e38851e5 31 extern int stp_sensor_pos[TGT_SENSOR_QUAN];
spm71 20:d23bcd97f2c5 32
spm71 49:80d4ffabec16 33 int stp_cur_pos = STP_POS_UNKN;
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 53:389fe53b2642 81 static int index = 0;
spm71 20:d23bcd97f2c5 82 if (direction == STP_CW) {
spm71 49:80d4ffabec16 83
spm71 53:389fe53b2642 84 if (stp_cur_pos <= 400) {
spm71 50:e3a03bc1e1a6 85 if (stp_cur_pos != STP_POS_UNKN){
spm71 50:e3a03bc1e1a6 86 //pc.printf("current position = %d\n", stp_cur_pos);
spm71 49:80d4ffabec16 87 stp_cur_pos++;
spm71 50:e3a03bc1e1a6 88 }
spm71 49:80d4ffabec16 89 }
spm71 49:80d4ffabec16 90 else {
spm71 52:8987e38851e5 91 pc.printf("Error: Cannot turn past maximum position. See stp_step() function.\n");
spm71 49:80d4ffabec16 92 while(1);
spm71 49:80d4ffabec16 93 }
spm71 49:80d4ffabec16 94
spm71 53:389fe53b2642 95 if (index == 3)
spm71 53:389fe53b2642 96 index = 0;
spm71 53:389fe53b2642 97 else
spm71 53:389fe53b2642 98 index++;
spm71 53:389fe53b2642 99 wait(MOTOR_DELAY);
spm71 53:389fe53b2642 100 spi_send(drv8806, turn[index]);
spm71 50:e3a03bc1e1a6 101 wait(TURN_DELAY);
spm71 20:d23bcd97f2c5 102 }
spm71 20:d23bcd97f2c5 103 else if (direction == STP_CCW) {
spm71 49:80d4ffabec16 104
spm71 49:80d4ffabec16 105 if (stp_cur_pos != 0) {
spm71 49:80d4ffabec16 106 if (stp_cur_pos != STP_POS_UNKN)
spm71 49:80d4ffabec16 107 stp_cur_pos--;
spm71 49:80d4ffabec16 108 }
spm71 49:80d4ffabec16 109 else {
spm71 52:8987e38851e5 110 pc.printf("Error: Cannot turn past home position. See stp_step() function.\n");
spm71 49:80d4ffabec16 111 wait(0.5);
spm71 49:80d4ffabec16 112 return;
spm71 49:80d4ffabec16 113 }
spm71 49:80d4ffabec16 114
spm71 53:389fe53b2642 115 if (index == 0)
spm71 53:389fe53b2642 116 index = 3;
spm71 53:389fe53b2642 117 else
spm71 53:389fe53b2642 118 index--;
spm71 53:389fe53b2642 119 wait(MOTOR_DELAY);
spm71 53:389fe53b2642 120 spi_send(drv8806, turn[index]);
spm71 50:e3a03bc1e1a6 121 wait(TURN_DELAY);
spm71 20:d23bcd97f2c5 122 }
spm71 41:9b293b14b845 123 wait(MOTOR_DELAY);
spm71 36:ad2b3d6f0e5a 124 }
spm71 36:ad2b3d6f0e5a 125
spm71 36:ad2b3d6f0e5a 126 /*
spm71 36:ad2b3d6f0e5a 127 * void step_test();
spm71 36:ad2b3d6f0e5a 128 * Description: tests the stepper motor
spm71 36:ad2b3d6f0e5a 129 *
spm71 36:ad2b3d6f0e5a 130 * Inputs:
spm71 36:ad2b3d6f0e5a 131 * Parameters:
spm71 36:ad2b3d6f0e5a 132 * Globals:
spm71 36:ad2b3d6f0e5a 133 *
spm71 36:ad2b3d6f0e5a 134 * Outputs:
spm71 36:ad2b3d6f0e5a 135 * Returns: void
spm71 36:ad2b3d6f0e5a 136 */
spm71 36:ad2b3d6f0e5a 137 void step_test() {
spm71 43:3b7f326aa096 138 stp_init();
spm71 41:9b293b14b845 139 while (uti_chk_ubutton() == 0);
spm71 46:85c4b722baa7 140 pc.printf("step motor test begin\n");
spm71 36:ad2b3d6f0e5a 141 while(1) {
spm71 36:ad2b3d6f0e5a 142 if (jog_ccw == 0) {
spm71 49:80d4ffabec16 143 stp_step(STP_CCW);
spm71 36:ad2b3d6f0e5a 144 }
spm71 36:ad2b3d6f0e5a 145 if (jog_cw == 0) {
spm71 50:e3a03bc1e1a6 146 stp_step(STP_CW);
spm71 36:ad2b3d6f0e5a 147 }
spm71 36:ad2b3d6f0e5a 148 if (cal_button == 0) {
spm71 36:ad2b3d6f0e5a 149 stp_find_home();
spm71 36:ad2b3d6f0e5a 150 }
spm71 46:85c4b722baa7 151 if (uti_chk_ubutton() == 1)
spm71 46:85c4b722baa7 152 break;
spm71 36:ad2b3d6f0e5a 153 }
spm71 36:ad2b3d6f0e5a 154 }
spm71 36:ad2b3d6f0e5a 155
spm71 41:9b293b14b845 156 /*
spm71 41:9b293b14b845 157 * void stp_find_home();
spm71 41:9b293b14b845 158 * Description: uses the stepper motor and home sensor to find home
spm71 41:9b293b14b845 159 *
spm71 41:9b293b14b845 160 * Inputs:
spm71 41:9b293b14b845 161 * Parameters:
spm71 41:9b293b14b845 162 * Globals:
spm71 41:9b293b14b845 163 *
spm71 41:9b293b14b845 164 * Outputs:
spm71 41:9b293b14b845 165 * Returns: void
spm71 41:9b293b14b845 166 */
spm71 36:ad2b3d6f0e5a 167 void stp_find_home() {
spm71 41:9b293b14b845 168 int count = 0;
spm71 41:9b293b14b845 169 int half_count = 0;
spm71 41:9b293b14b845 170 stp_cur_pos = STP_POS_UNKN;
spm71 41:9b293b14b845 171 //pc.printf("Home sensor is currently %d\n", home_sensor.read());
spm71 41:9b293b14b845 172 if (home_sensor == 0) {
spm71 41:9b293b14b845 173 for(int i = 0; i < 100; i++)
spm71 41:9b293b14b845 174 stp_step(STP_CW);
spm71 41:9b293b14b845 175 if (home_sensor == 0) {
spm71 52:8987e38851e5 176 pc.printf("Error: Home sensor not functioning. See stp_find_home() function.\n", home_sensor.read());
spm71 41:9b293b14b845 177 while(1);
spm71 41:9b293b14b845 178 }
spm71 41:9b293b14b845 179 }
spm71 41:9b293b14b845 180 while (home_sensor.read() != 0) {
spm71 41:9b293b14b845 181 stp_step(STP_CCW);
spm71 41:9b293b14b845 182 }
spm71 41:9b293b14b845 183 while (home_sensor.read() != 1) {
spm71 41:9b293b14b845 184 stp_step(STP_CCW);
spm71 41:9b293b14b845 185 count++;
spm71 41:9b293b14b845 186 }
spm71 41:9b293b14b845 187 half_count = count/2;
spm71 41:9b293b14b845 188 for(int i = 0; i < half_count; i++)
spm71 41:9b293b14b845 189 stp_step(STP_CW);
spm71 41:9b293b14b845 190 stp_cur_pos = 0;
spm71 41:9b293b14b845 191 pc.printf("Home found.\n");
phn10 44:4c2ba5bbba67 192 }
phn10 44:4c2ba5bbba67 193
phn10 44:4c2ba5bbba67 194
phn10 44:4c2ba5bbba67 195 /*
phn10 44:4c2ba5bbba67 196 * void stp_calibrate(int station, float * sensor_values, int * cal_status);
phn10 44:4c2ba5bbba67 197 * Description: uses the stepper motor and home sensor to find home
phn10 44:4c2ba5bbba67 198 *
phn10 44:4c2ba5bbba67 199 * Inputs:
phn10 44:4c2ba5bbba67 200 * Parameters:
phn10 47:0547118e348e 201 * int station: STATION_A or STATION_B
phn10 47:0547118e348e 202 * float *sensor_value: array of float holds 16 sensor values
phn10 47:0547118e348e 203 * int *cal_status: pointer to int variable that will hold calibration status
phn10 44:4c2ba5bbba67 204 * Globals:
phn10 44:4c2ba5bbba67 205 *
phn10 44:4c2ba5bbba67 206 * Outputs:
phn10 44:4c2ba5bbba67 207 * Returns: void
phn10 44:4c2ba5bbba67 208 */
phn10 47:0547118e348e 209 void stp_calibrate(int station, float * sensor_values, int * cal_status){
spm71 49:80d4ffabec16 210 while (uti_chk_ubutton() == 0);
spm71 49:80d4ffabec16 211 pc.printf("step 9 test begin\n");
spm71 49:80d4ffabec16 212
phn10 47:0547118e348e 213 int sensor_no = 0;
spm71 56:048b30c9f2a1 214 if (station == STATION_A) sensor_no = 8;
spm71 54:d5a95bc8ffb0 215 if (station == STATION_B) sensor_no = 0;
phn10 47:0547118e348e 216
spm71 53:389fe53b2642 217
spm71 53:389fe53b2642 218 pc.printf("sensor_no: %d\n", sensor_no);
spm71 53:389fe53b2642 219
spm71 49:80d4ffabec16 220 // find home position
phn10 47:0547118e348e 221 stp_find_home();
phn10 47:0547118e348e 222
spm71 51:1eb60f0d2f03 223
phn10 47:0547118e348e 224 // turn laser on
spm71 49:80d4ffabec16 225 wait(1);
phn10 47:0547118e348e 226 lzr_on();
phn10 47:0547118e348e 227
phn10 47:0547118e348e 228 for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
phn10 47:0547118e348e 229 // scan all 16 sensors into sensor_values array
phn10 47:0547118e348e 230 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
phn10 47:0547118e348e 231
phn10 47:0547118e348e 232 // keep turning stepper motor clock wise until it points to a PT sensor
spm71 53:389fe53b2642 233 while (sensor_values[sensor_no + i] * 3.3f < PTTHRESH) {
phn10 47:0547118e348e 234 // turn CW one step
phn10 47:0547118e348e 235 stp_step(STP_CW);
phn10 47:0547118e348e 236
phn10 47:0547118e348e 237 wait(0.005);
phn10 47:0547118e348e 238
phn10 47:0547118e348e 239 // scan all PT sensors again
phn10 47:0547118e348e 240 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
spm71 51:1eb60f0d2f03 241
spm71 51:1eb60f0d2f03 242 for (int j = 0; j < TGT_SENSOR_QUAN; j++) {
spm71 53:389fe53b2642 243 if (sensor_values[sensor_no + j] * 3.3f > PTTHRESH) {
spm71 53:389fe53b2642 244 pc.printf("PT %d: %f\n", sensor_no + j, sensor_values[sensor_no + j] * 3.3f);
spm71 53:389fe53b2642 245 wait(0.02);
spm71 53:389fe53b2642 246 }
spm71 51:1eb60f0d2f03 247 }
phn10 47:0547118e348e 248 }
phn10 47:0547118e348e 249 // found the sensor, save it's position
phn10 47:0547118e348e 250 stp_sensor_pos[i] = stp_cur_pos;
phn10 47:0547118e348e 251 }
phn10 47:0547118e348e 252 // found position of all 8 sensors
phn10 47:0547118e348e 253
phn10 47:0547118e348e 254 // go back home
phn10 47:0547118e348e 255 stp_find_home();
phn10 47:0547118e348e 256
phn10 47:0547118e348e 257 *cal_status = CALIBRATED;
phn10 47:0547118e348e 258
phn10 47:0547118e348e 259 // turn laser off
phn10 47:0547118e348e 260 lzr_off();
phn10 47:0547118e348e 261
phn10 47:0547118e348e 262 // for debugging: print positions of all 8 sensors here
phn10 47:0547118e348e 263 for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
phn10 47:0547118e348e 264 pc.printf("PT %d: %d ", i, stp_sensor_pos[i]);
phn10 47:0547118e348e 265 }
spm71 52:8987e38851e5 266 }
spm71 52:8987e38851e5 267
spm71 52:8987e38851e5 268 /*
spm71 52:8987e38851e5 269 * void repeatability_test(itn sensor_position, int cal_status);
spm71 52:8987e38851e5 270 * Description: repeatability test in part 10 of lab 6. The function will point to laser to
spm71 52:8987e38851e5 271 * the location of the specified sensor_position arguemnt. The function will
spm71 52:8987e38851e5 272 * return error when the calibration status is NOT_CALIBRATED
spm71 52:8987e38851e5 273 *
spm71 52:8987e38851e5 274 * Inputs:
spm71 52:8987e38851e5 275 * Parameters:
spm71 54:d5a95bc8ffb0 276 * int sensor_position: the position of sensor that the laser will point to (range from 0 to 7)
spm71 52:8987e38851e5 277 * int cal_status: calibration status.
spm71 52:8987e38851e5 278 * Globals:
spm71 52:8987e38851e5 279 *
spm71 52:8987e38851e5 280 * Outputs:
spm71 52:8987e38851e5 281 * Returns: void
spm71 52:8987e38851e5 282 */
spm71 52:8987e38851e5 283 void repeatability_test(int sensor_position, int cal_status) {
spm71 52:8987e38851e5 284 int num_steps = 0; // number of steps the stepper motor needed to move from current position
spm71 52:8987e38851e5 285
phn10 55:1a25dd75e309 286 //pc.printf("stp_cur_pos: %d\t stp_sensor_pos[sensor_position]: %d\t sensor_position: %d\n", stp_cur_pos, stp_sensor_pos[sensor_position], sensor_position);
phn10 55:1a25dd75e309 287 pc.printf("cal_status: %d\n", cal_status);
spm71 52:8987e38851e5 288 // if the system is not calibrated
phn10 55:1a25dd75e309 289 if (cal_status == NOT_CALIBRATED) {
spm71 52:8987e38851e5 290 pc.printf("Error: The system is not calibrated. See repeatibility_test() function .\n");
phn10 55:1a25dd75e309 291 while (1);
phn10 55:1a25dd75e309 292 }
spm71 52:8987e38851e5 293 else {
spm71 52:8987e38851e5 294 // if the current position is less than the position of sepcified sensor
spm71 52:8987e38851e5 295 if (stp_cur_pos < stp_sensor_pos[sensor_position]) {
spm71 52:8987e38851e5 296 num_steps = stp_sensor_pos[sensor_position] - stp_cur_pos;
spm71 52:8987e38851e5 297
spm71 54:d5a95bc8ffb0 298 for (int i = 0; i < num_steps; i++) {
spm71 52:8987e38851e5 299 stp_step(STP_CW);
spm71 52:8987e38851e5 300 }
spm71 52:8987e38851e5 301 } else {
spm71 52:8987e38851e5 302 num_steps = stp_cur_pos - stp_sensor_pos[sensor_position];
spm71 52:8987e38851e5 303
spm71 54:d5a95bc8ffb0 304 for (int i = 0; i < num_steps; i++) {
spm71 52:8987e38851e5 305 stp_step(STP_CCW);
spm71 52:8987e38851e5 306 }
spm71 52:8987e38851e5 307 }
spm71 52:8987e38851e5 308 }
phn10 47:0547118e348e 309 }