Stepper motor tester version 1 for the STM32F303K8 Nucleo development board

Dependencies:   RPCInterface mbed-dsp mbed

main.cpp

Committer:
rcflyair
Date:
2016-05-19
Revision:
1:eabd95ed3f53
Parent:
0:5e49c3bc05f5
Child:
2:095a45ed34c5

File content as of revision 1:eabd95ed3f53:

#include "mbed.h"
#include "SerialRPCInterface.h"
#include "L6470.h"
#include "HC595.h"

// Global objects
DigitalOut led_red(LED_RED);                                    // active low to turn on red part of RGB LED on FRDM-K22F
DigitalOut led_green(LED_GREEN);                                // active low to turn on green part of RGB LED on FRDM-K22F
DigitalOut led_blue(LED_BLUE);                                  // active low to turn on blue part of RGB LED on FRDM-K22F

SerialRPCInterface rpc(USBTX, USBRX, 115200);                   // create serial rpc interface on usb virtual com port
L6470 motor(PTD6, PTD7, PTD5, PTD4);                            // create motor object with mosi, miso, sclk, cs on FRDM-K22F
int position;                                                   // current motor position from L6470 in steps
int mode;                                                       // current mode is either manual or auto
int range;
int closeMotorPosition;
int openMotorPosition;
int lastPosition;

HC595 hc595(PTC5, PTC10, PTC7);                                 // create led bar driver with ser, oe, rclk, and srclk
AnalogIn lightSense(PTB0);                                      // analog voltage from light sensor on PDU
PwmOut brightness(PTA5);                                        // output to external pdu contols PMM level for brightness

DigitalIn sw0(PTC9);
DigitalIn sw1(PTC8);
DigitalIn sw2(PTA12);
DigitalIn sw3(PTA13);

float ledbar = 1;

// RPC Function calls
void GetStatus(Arguments *input, Reply *output)
{
    led_green = !led_green;
    int temp = motor.GetStatus();
    output->putData(temp & 0xFFFFFF);
};

/*void GetABSPOS(Arguments *input, Reply *output)
{
    led_green = !led_green;
    // uint32_t temp = motor.(ABS_POS);     // rpc will not except uint32_t by default
    int temp = motor.GetParameter(ABS_POS);
    temp = (temp & 0x3FFFFF);
    output->putData(temp);
};
*/

// motor.move(direction, step)
void Move(Arguments *input, Reply *output)
{
    led_green = !led_green;
    int arg0 = input->getArg<int>();
    int arg1 = input->getArg<int>();
    motor.Move(arg0, arg1);
};

void Run(Arguments *input, Reply *output)
{
    led_green = !led_green;
    int arg0 = input->getArg<int>();
    motor.Run(arg0);
};
void Stop(Arguments *input, Reply *output)
{
    led_green = !led_green;
    motor.HardStop();
};

void ResetPosition(Arguments *input, Reply *output)
{
    led_green = !led_green;
    motor.ResetPosition();
};

void SetParameter(Arguments *input, Reply *output)
{
    led_green = !led_green;
    // uint8_t arg0 = input->getArg<int>();
    // uint32_t arg1 = input->getArg<int>();
    int arg0 = input->getArg<int>();
    int arg1 = input->getArg<int>();
    motor.SetParameter(arg0, arg1);
};

void SetLEDBar(Arguments *input, Reply *output)
{
    led_green = !led_green;
    int arg0 = input->getArg<int>();
    hc595.bar(arg0);
    //output->putData(arg0);
}

void GetParameter(Arguments *input, Reply *output)
{
    led_green = !led_green;
    //uint8_t arg0 = input->getArg<int>();      // rpc will not accept uint8_t by default
    //uint32_t temp = motor.GetParameter(arg0); // rpc will not accept uint32_t by default
    int arg0 = input->getArg<int>();
    int temp = motor.GetParameter(arg0);
    output->putData(temp);
}
// RPC variable assignments
RPCVariable<int> rpc_position(&position, "position");
RPCVariable<int> rpc_mode(&mode, "mode");
RPCVariable<int> rpc_range(&range, "range");
RPCVariable<int> rpc_openMotorPosition(&openMotorPosition, "openMotorPosition");
RPCVariable<int> rpc_closeMotorPostion(&closeMotorPosition, "closeMotorPosition");
RPCVariable<int> rpc_lastPosition(&lastPosition, "lastPosition");

// RPC function assignments
RPCFunction rpc_GetStatus(&GetStatus, "GetStatus");
//RPCFunction rpc_GetABSPOS(&GetABSPOS, "GetABSPOS");
RPCFunction rpc_Move(&Move, "Move");
RPCFunction rpc_Run(&Run, "Run");
RPCFunction rpc_Stop(&Stop, "Stop");
RPCFunction rpc_ResetPosition(&ResetPosition, "ResetPosition");
RPCFunction rpc_SetParameter(&SetParameter, "SetParameter");
RPCFunction rpc_GetParameter(&GetParameter, "GetParameter");
RPCFunction rpc_SetLEDBar(&SetLEDBar, "SetLEDBar");

int main(){
    // Turn off LED's
    led_red = 1; led_green = 1; led_blue = 1;
    
    // set motor parameters
    motor.Reset();
    motor.HardStop();
    motor.SetParameter(ACC, 0xFF);
    motor.SetParameter(DEC, 0xFF);
    motor.SetParameter(MAX_SPEED, 0x20);
    motor.SetParameter(MIN_SPEED, 0x00);
    motor.SetParameter(KVAL_HOLD, 0x29);
    motor.SetParameter(KVAL_RUN, 0x29);
    motor.SetParameter(KVAL_ACC, 0x29);
    motor.SetParameter(KVAL_DEC, 0x29);
    
    motor.SetParameter(STALL_TH, 0x18);
    
    // move motor OPEN to find motor limit with overcurrent detection
    int status = 0;
    int stall = 0;
    int FindMotorLimitsCounter = 0;
    motor.Run(OPEN);
    // poll the motor status register for stall
    //  increment counter for timeout detection
    while ((!stall) & (FindMotorLimitsCounter < 80)){
        status = motor.GetStatus();                                         // read status register
        stall = !((status >> 13) & 0x01) || !((status >> 14) & 0x01);       // check for motor stall
        FindMotorLimitsCounter++;                                           // increment the timeout counter
        wait_ms(100);
    }    
    motor.HardStop();                                                       // either motor stall or timeout has occured, so stop motor
    status = motor.GetStatus();                                             // clear status register by reading
    
    // if we have timed out, then flash red led forever (halt)
    if (FindMotorLimitsCounter >= 80){
        while(1){
            led_green = 1; led_blue = 1; led_red = !led_red;
            wait_ms(100);
            }
        }
 
    // reset the motor position
    motor.ResetPosition();
        
    // store position of motor fully open - should be zero
    openMotorPosition = motor.GetPosition();
    
    // move motor CLOSE to find motor limit with overcurrent detection
    status = 0;
    stall = 0;
    FindMotorLimitsCounter = 0;
    //motor.SetParameter(STALL_TH, 0x18);   // only used if close stall threshold is different than open threshold
    motor.Run(CLOSE);
    // poll the motor status register for stall
    //  increment counter for timeout detection
    while ((!stall) & (FindMotorLimitsCounter < 80)){
        status = motor.GetStatus();                                         // read the status register
        stall = !((status >> 13) & 0x01) || !((status >> 14) & 0x01);       // check for motor stall
        FindMotorLimitsCounter++;                                           // increment the timeout counter
        wait_ms(100);                                                       // poll loop delay
    }    
    motor.HardStop();
    status = motor.GetStatus();
    if (FindMotorLimitsCounter >= 80){                                      // if has not stalled yet, display flashing red as error
        while(1){
            led_green = !led_green; led_blue = 1; led_red = 1;
            wait_ms(100);
            }
        }
    led_green = 1; led_blue = 1; led_red = 1;
    // store position of motor fully close
    //** hardcore test rpc connection **\\     closeMotorPosition = 200000;//motor.GetParameter(ABS_POS);
    closeMotorPosition = motor.GetPosition();

    // calculate motor movement range
    range = closeMotorPosition - openMotorPosition;
    float motorPercent = range / 100;
    motor.Move(OPEN, range);
    lastPosition = motor.GetPosition();
    
    while(1){
        //status = motor.GetStatus();
        position = motor.GetPosition();
        if (position < openMotorPosition)
            position = lastPosition;
        if (position > closeMotorPosition)
            position = lastPosition;
        lastPosition = position;
        
        uint32_t p = (position - openMotorPosition)/motorPercent;
        hc595.bar(p/10+1);

        // check for auto mode
        if (!sw0){
            hc595.auto_led(true);
            mode = 1;   // automatic mode
        }
        else
            hc595.auto_led(false);

        // check for manual mode
        if (!sw1){
            hc595.manual_led(true);
            mode = 0;
        }
        else
            hc595.manual_led(false);

        // manual mode and position switch requests close
        if ((!sw2)&&(!sw1)) {
            if (position + 1000 < closeMotorPosition) motor.Move(CLOSE, 1000);
            else if ((closeMotorPosition - position) > 0) motor.Move(CLOSE, closeMotorPosition - position);
            }

        // manual mode and position switch requests open
        if ((!sw3)&&(!sw1)) {
            if (position - 1000 > 0) motor.Move(OPEN, 1000);
            else if (position > 0) motor.Move(OPEN, position);
            }
        wait_ms(100);
        
    }
}