Lab 6 code.

Dependencies:   mbed

Fork of WaG by GroupA

stepper.cpp

Committer:
spm71
Date:
2018-03-07
Revision:
21:88f9f280931b
Parent:
20:d23bcd97f2c5
Child:
22:09dd6977576b

File content as of revision 21:88f9f280931b:

/******************************************************************************
* 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/02/2018
*
******************************************************************************/

#include "mbed.h"
#include "io_pins.h"
#include "spi.h"
#include "stepper.h"

DigitalOut stp_ncs(STP_DRV8806_NCS);
extern DigitalIn jog_ccw;
extern DigitalIn jog_cw;

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++)
            spi_send(drv8806, turn[i]);
    }
    else if (direction == STP_CCW) {
        for (int i = 3; i >= 0; i--)
            spi_send(drv8806, turn[i]);
    }
    
}