Lab 6 code.
Dependencies: mbed
Fork of WaG by
Diff: stepper.cpp
- Revision:
- 41:9b293b14b845
- Parent:
- 36:ad2b3d6f0e5a
- Child:
- 42:6cba679a4ee4
--- a/stepper.cpp Fri Mar 23 21:24:26 2018 +0000 +++ b/stepper.cpp Tue Mar 27 16:34:17 2018 +0000 @@ -8,7 +8,7 @@ * Purpose: Driver for stepper motor * * Created: 03/02/2018 -* Last Modified: 03/08/2018 +* Last Modified: 03/27/2018 * ******************************************************************************/ @@ -16,11 +16,13 @@ #include "io_pins.h" #include "spi.h" #include "stepper.h" +#include "utility.h" extern DigitalIn jog_ccw; extern DigitalIn jog_cw; extern DigitalIn my_button; extern DigitalIn cal_button; +extern DigitalIn home_sensor; extern Serial pc; int stp_cur_pos; @@ -65,25 +67,24 @@ * Returns: void */ void stp_step(int direction) { - jog_cw.mode(PullUp); - jog_ccw.mode(PullUp); //static int cur_pos = stp_cur_pos; static int turn[4] = {0x03, 0x06, 0x0c, 0x09}; if (direction == STP_CW) { for (int i = 0; i < 4; i++) { - wait(0.002); + wait(MOTOR_DELAY); //pc.printf("i = %d\n", i); spi_send(drv8806, turn[i]); } } else if (direction == STP_CCW) { for (int i = 3; i >= 0; i--) { - wait(0.002); + wait(MOTOR_DELAY); //pc.printf("i = %d\n", i); spi_send(drv8806, turn[i]); } } + wait(MOTOR_DELAY); } /* @@ -98,19 +99,62 @@ * Returns: void */ void step_test() { + jog_cw.mode(PullUp); + jog_ccw.mode(PullUp); + cal_button.mode(PullUp); + home_sensor.mode(PullUp); + while (uti_chk_ubutton() == 0); + pc.printf("test begin\n"); while(1) { if (jog_ccw == 0) { stp_step(STP_CCW); + //pc.printf("Home sensor is currently %d\n", home_sensor.read()); } if (jog_cw == 0) { stp_step(STP_CW); + //pc.printf("Home sensor is currently %d\n", home_sensor.read()); } if (cal_button == 0) { stp_find_home(); + //pc.printf("Home sensor is currently %d\n", home_sensor.read()); } } } +/* + * void stp_find_home(); + * Description: uses the stepper motor and home sensor to find home + * + * Inputs: + * Parameters: + * Globals: + * + * Outputs: + * Returns: void +*/ void stp_find_home() { - + int count = 0; + int half_count = 0; + stp_cur_pos = STP_POS_UNKN; + //pc.printf("Home sensor is currently %d\n", home_sensor.read()); + if (home_sensor == 0) { + for(int i = 0; i < 100; i++) + stp_step(STP_CW); + if (home_sensor == 0) { + pc.printf("Error, home sensor not functioning. Home sensor is currently %d\n", home_sensor.read()); + while(1); + } + } + while (home_sensor.read() != 0) { + stp_step(STP_CCW); + } + while (home_sensor.read() != 1) { + stp_step(STP_CCW); + count++; + } + half_count = count/2; + for(int i = 0; i < half_count; i++) + stp_step(STP_CW); + stp_cur_pos = 0; + pc.printf("Home found.\n"); } \ No newline at end of file