Lab 6 code.
Dependencies: mbed
Fork of WaG by
Diff: stepper.cpp
- Revision:
- 52:8987e38851e5
- Parent:
- 51:1eb60f0d2f03
- Child:
- 53:389fe53b2642
--- a/stepper.cpp Tue Apr 03 20:51:20 2018 +0000 +++ b/stepper.cpp Tue Apr 03 22:32:41 2018 +0000 @@ -28,9 +28,9 @@ extern DigitalIn home_sensor; extern Serial pc; extern DigitalOut laser; +extern int stp_sensor_pos[TGT_SENSOR_QUAN]; int stp_cur_pos = STP_POS_UNKN; -int stp_sensor_pos[TGT_SENSOR_QUAN]; extern spi_cfg drv8806 { SPI_DRV8806_ID, @@ -87,7 +87,7 @@ } } else { - pc.printf("Cannot turn past maximum position. Fatal error.\n"); + pc.printf("Error: Cannot turn past maximum position. See stp_step() function.\n"); while(1); } @@ -106,7 +106,7 @@ stp_cur_pos--; } else { - pc.printf("Cannot turn past home position.\n"); + pc.printf("Error: Cannot turn past home position. See stp_step() function.\n"); wait(0.5); return; } @@ -171,7 +171,7 @@ for(int i = 0; i < 100; i++) stp_step(STP_CW); if (home_sensor == 0) { - pc.printf("Error, home sensor not functioning. Fatal error.\n", home_sensor.read()); + pc.printf("Error: Home sensor not functioning. See stp_find_home() function.\n", home_sensor.read()); while(1); } } @@ -257,4 +257,43 @@ for (int i = 0; i < TGT_SENSOR_QUAN; i++) { pc.printf("PT %d: %d ", i, stp_sensor_pos[i]); } +} + +/* + * void repeatability_test(itn sensor_position, int cal_status); + * Description: repeatability test in part 10 of lab 6. The function will point to laser to + * the location of the specified sensor_position arguemnt. The function will + * return error when the calibration status is NOT_CALIBRATED + * + * Inputs: + * Parameters: + * int sensor_position: the position of sensor that the laser will point to (range from 0 to 15) + * int cal_status: calibration status. + * Globals: + * + * Outputs: + * Returns: void +*/ +void repeatability_test(int sensor_position, int cal_status) { + int num_steps = 0; // number of steps the stepper motor needed to move from current position + + // if the system is not calibrated + if (cal_status == NOT_CALIBRATED) + pc.printf("Error: The system is not calibrated. See repeatibility_test() function .\n"); + else { + // if the current position is less than the position of sepcified sensor + if (stp_cur_pos < stp_sensor_pos[sensor_position]) { + num_steps = stp_sensor_pos[sensor_position] - stp_cur_pos; + + for (int i = 0; num_steps; i++) { + stp_step(STP_CW); + } + } else { + num_steps = stp_cur_pos - stp_sensor_pos[sensor_position]; + + for (int i = 0; num_steps; i++) { + stp_step(STP_CCW); + } + } + } } \ No newline at end of file