Lab 6 code.
Dependencies: mbed
Fork of WaG by
stepper.cpp
- Committer:
- spm71
- Date:
- 2018-03-30
- Revision:
- 46:85c4b722baa7
- Parent:
- 45:54d18ab80fd1
- Child:
- 48:d612de6880b0
File content as of revision 46:85c4b722baa7:
/****************************************************************************** * EECS 397 * * Assignment Name: Lab 6: WaG * * Authors: Sam Morrison and Phong Nguyen * File name: stepper.cpp * Purpose: Driver for stepper motor * * Created: 03/02/2018 * Last Modified: 03/29/2018 * ******************************************************************************/ #include "mbed.h" #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; int stp_sensor_pos[TGT_SENSOR_QUAN]; extern spi_cfg drv8806 { SPI_DRV8806_ID, STP_DRV8806_NCS, DRV8806_SPI_MODE, DRV8806_SPI_FREQ, DRV8806_SPI_NO_BITS, }; /* * void stp_init(); * Description: initializes stepper values to unkown * * Inputs: * Parameters: void * Globals: * * Outputs: * Returns: void */ void stp_init() { stp_cur_pos = STP_POS_UNKN; jog_cw.mode(PullUp); jog_ccw.mode(PullUp); cal_button.mode(PullUp); home_sensor.mode(PullUp); for (int i = 1; i <= TGT_SENSOR_QUAN; i++) { //stp_sensor_pos[i] = STP_POS_UNKN; } } /* * void stp_step(int direction); * Description: turns the stepper motor clockwise or counter-clockwise * * Inputs: * Parameters: * int direction * Globals: * * Outputs: * Returns: void */ void stp_step(int direction) { //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(MOTOR_DELAY); //pc.printf("i = %d\n", i); spi_send(drv8806, turn[i]); } wait(0.015); } else if (direction == STP_CCW) { for (int i = 3; i >= 0; i--) { wait(MOTOR_DELAY); //pc.printf("i = %d\n", i); spi_send(drv8806, turn[i]); } wait(0.015); } wait(MOTOR_DELAY); } /* * void step_test(); * Description: tests the stepper motor * * Inputs: * Parameters: * Globals: * * Outputs: * Returns: void */ void step_test() { stp_init(); while (uti_chk_ubutton() == 0); pc.printf("step motor test begin\n"); while(1) { if (jog_ccw == 0) { if (stp_cur_pos != 0) { stp_step(STP_CCW); if (stp_cur_pos != STP_POS_UNKN) stp_cur_pos--; } else { pc.printf("Cannot turn past home position.\n"); wait(0.02); } } if (jog_cw == 0) { if (stp_cur_pos <= 400) { stp_step(STP_CW); if (stp_cur_pos != STP_POS_UNKN) stp_cur_pos++; } else { pc.printf("Cannot turn past maximum position. Fatal error.\n"); while(1); } } if (cal_button == 0) { stp_find_home(); } if (uti_chk_ubutton() == 1) break; } } /* * 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. Fatal error.\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"); } /* * void stp_calibrate(int station, float * sensor_values, int * cal_status); * Description: uses the stepper motor and home sensor to find home * * Inputs: * Parameters: * Globals: * * Outputs: * Returns: void */ void stp_calibrate(int station, float * sensor_values, int * cal_status);