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
- Committer:
- spm71
- Date:
- 2018-03-22
- Revision:
- 35:ad2b3d6f0e5a
- Parent:
- 22:09dd6977576b
- Child:
- 41:9b293b14b845
File content as of revision 35:ad2b3d6f0e5a:
/******************************************************************************
* EECS 397
*
* Assignment Name: Lab 5: WaG
*
* Authors: Sam Morrison and Phong Nguyen
* File name: stepper.cpp
* Purpose: Driver for stepper motor
*
* Created: 03/02/2018
* Last Modified: 03/08/2018
*
******************************************************************************/
#include "mbed.h"
#include "io_pins.h"
#include "spi.h"
#include "stepper.h"
extern DigitalIn jog_ccw;
extern DigitalIn jog_cw;
extern DigitalIn my_button;
extern DigitalIn cal_button;
extern Serial pc;
int stp_cur_pos;
int stp_sensor_pos[NUM_SENSORS + 1];
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;
for (int i = 1; i <= NUM_SENSORS; 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) {
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);
//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);
//pc.printf("i = %d\n", i);
spi_send(drv8806, turn[i]);
}
}
}
/*
* void step_test();
* Description: tests the stepper motor
*
* Inputs:
* Parameters:
* Globals:
*
* Outputs:
* Returns: void
*/
void step_test() {
while(1) {
if (jog_ccw == 0) {
stp_step(STP_CCW);
}
if (jog_cw == 0) {
stp_step(STP_CW);
}
if (cal_button == 0) {
stp_find_home();
}
}
}
void stp_find_home() {
}
