Stepper motor tester version 1 for the STM32F303K8 Nucleo development board
Dependencies: RPCInterface mbed-dsp mbed
main.cpp@0:5e49c3bc05f5, 2016-05-11 (annotated)
- Committer:
- rcflyair
- Date:
- Wed May 11 16:58:41 2016 +0000
- Revision:
- 0:5e49c3bc05f5
- Child:
- 1:eabd95ed3f53
K22_GAMI_eMPC_TestCell_v1b development commit 01
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rcflyair | 0:5e49c3bc05f5 | 1 | #include "mbed.h" |
rcflyair | 0:5e49c3bc05f5 | 2 | #include "SerialRPCInterface.h" |
rcflyair | 0:5e49c3bc05f5 | 3 | #include "L6470.h" |
rcflyair | 0:5e49c3bc05f5 | 4 | #include "HC595.h" |
rcflyair | 0:5e49c3bc05f5 | 5 | |
rcflyair | 0:5e49c3bc05f5 | 6 | // Global objects |
rcflyair | 0:5e49c3bc05f5 | 7 | DigitalOut led_red(LED_RED); // active low to turn on red part of RGB LED on FRDM-K22F |
rcflyair | 0:5e49c3bc05f5 | 8 | DigitalOut led_green(LED_GREEN); // active low to turn on green part of RGB LED on FRDM-K22F |
rcflyair | 0:5e49c3bc05f5 | 9 | DigitalOut led_blue(LED_BLUE); // active low to turn on blue part of RGB LED on FRDM-K22F |
rcflyair | 0:5e49c3bc05f5 | 10 | |
rcflyair | 0:5e49c3bc05f5 | 11 | SerialRPCInterface rpc(USBTX, USBRX, 115200); // create serial rpc interface on usb virtual com port |
rcflyair | 0:5e49c3bc05f5 | 12 | L6470 motor(PTD6, PTD7, PTD5, PTD4); // create motor object with mosi, miso, sclk, cs on FRDM-K22F |
rcflyair | 0:5e49c3bc05f5 | 13 | |
rcflyair | 0:5e49c3bc05f5 | 14 | HC595 hc595(PTC5, PTC10, PTC7); // create led bar driver with ser, oe, rclk, and srclk |
rcflyair | 0:5e49c3bc05f5 | 15 | AnalogIn lightSense(PTB0); // analog voltage from light sensor on PDU |
rcflyair | 0:5e49c3bc05f5 | 16 | PwmOut brightness(PTA5); // brightness of PDU |
rcflyair | 0:5e49c3bc05f5 | 17 | DigitalIn sw0(PTC9); |
rcflyair | 0:5e49c3bc05f5 | 18 | DigitalIn sw1(PTC8); |
rcflyair | 0:5e49c3bc05f5 | 19 | DigitalIn sw2(PTA12); |
rcflyair | 0:5e49c3bc05f5 | 20 | DigitalIn sw3(PTA13); |
rcflyair | 0:5e49c3bc05f5 | 21 | |
rcflyair | 0:5e49c3bc05f5 | 22 | float ledbar = 1; |
rcflyair | 0:5e49c3bc05f5 | 23 | |
rcflyair | 0:5e49c3bc05f5 | 24 | // RPC Function calls |
rcflyair | 0:5e49c3bc05f5 | 25 | void GetStatus(Arguments *input, Reply *output) |
rcflyair | 0:5e49c3bc05f5 | 26 | { |
rcflyair | 0:5e49c3bc05f5 | 27 | led_green = !led_green; |
rcflyair | 0:5e49c3bc05f5 | 28 | int temp = motor.GetStatus(); |
rcflyair | 0:5e49c3bc05f5 | 29 | output->putData(temp & 0xFFFFFF); |
rcflyair | 0:5e49c3bc05f5 | 30 | }; |
rcflyair | 0:5e49c3bc05f5 | 31 | |
rcflyair | 0:5e49c3bc05f5 | 32 | void GetABSPOS(Arguments *input, Reply *output) |
rcflyair | 0:5e49c3bc05f5 | 33 | { |
rcflyair | 0:5e49c3bc05f5 | 34 | led_green = !led_green; |
rcflyair | 0:5e49c3bc05f5 | 35 | // uint32_t temp = motor.GetParameter(ABS_POS); |
rcflyair | 0:5e49c3bc05f5 | 36 | int temp = motor.GetParameter(ABS_POS); |
rcflyair | 0:5e49c3bc05f5 | 37 | temp = (temp & 0x3FFFFF); // mask 22 bit value |
rcflyair | 0:5e49c3bc05f5 | 38 | output->putData(temp); |
rcflyair | 0:5e49c3bc05f5 | 39 | }; |
rcflyair | 0:5e49c3bc05f5 | 40 | |
rcflyair | 0:5e49c3bc05f5 | 41 | // motor.move(direction, step) |
rcflyair | 0:5e49c3bc05f5 | 42 | void Move(Arguments *input, Reply *output) |
rcflyair | 0:5e49c3bc05f5 | 43 | { |
rcflyair | 0:5e49c3bc05f5 | 44 | led_green = !led_green; |
rcflyair | 0:5e49c3bc05f5 | 45 | int arg0 = input->getArg<int>(); |
rcflyair | 0:5e49c3bc05f5 | 46 | int arg1 = input->getArg<int>(); |
rcflyair | 0:5e49c3bc05f5 | 47 | motor.Move(arg0, arg1); |
rcflyair | 0:5e49c3bc05f5 | 48 | }; |
rcflyair | 0:5e49c3bc05f5 | 49 | |
rcflyair | 0:5e49c3bc05f5 | 50 | void Run(Arguments *input, Reply *output) |
rcflyair | 0:5e49c3bc05f5 | 51 | { |
rcflyair | 0:5e49c3bc05f5 | 52 | led_green = !led_green; |
rcflyair | 0:5e49c3bc05f5 | 53 | int arg0 = input->getArg<int>(); |
rcflyair | 0:5e49c3bc05f5 | 54 | motor.Run(arg0); |
rcflyair | 0:5e49c3bc05f5 | 55 | }; |
rcflyair | 0:5e49c3bc05f5 | 56 | void Stop(Arguments *input, Reply *output) |
rcflyair | 0:5e49c3bc05f5 | 57 | { |
rcflyair | 0:5e49c3bc05f5 | 58 | led_green = !led_green; |
rcflyair | 0:5e49c3bc05f5 | 59 | motor.HardStop(); |
rcflyair | 0:5e49c3bc05f5 | 60 | }; |
rcflyair | 0:5e49c3bc05f5 | 61 | |
rcflyair | 0:5e49c3bc05f5 | 62 | void ResetPosition(Arguments *input, Reply *output) |
rcflyair | 0:5e49c3bc05f5 | 63 | { |
rcflyair | 0:5e49c3bc05f5 | 64 | led_green = !led_green; |
rcflyair | 0:5e49c3bc05f5 | 65 | motor.ResetPosition(); |
rcflyair | 0:5e49c3bc05f5 | 66 | }; |
rcflyair | 0:5e49c3bc05f5 | 67 | |
rcflyair | 0:5e49c3bc05f5 | 68 | void SetParameter(Arguments *input, Reply *output) |
rcflyair | 0:5e49c3bc05f5 | 69 | { |
rcflyair | 0:5e49c3bc05f5 | 70 | led_green = !led_green; |
rcflyair | 0:5e49c3bc05f5 | 71 | // uint8_t arg0 = input->getArg<int>(); |
rcflyair | 0:5e49c3bc05f5 | 72 | // uint32_t arg1 = input->getArg<int>(); |
rcflyair | 0:5e49c3bc05f5 | 73 | int arg0 = input->getArg<int>(); |
rcflyair | 0:5e49c3bc05f5 | 74 | int arg1 = input->getArg<int>(); |
rcflyair | 0:5e49c3bc05f5 | 75 | motor.SetParameter(arg0, arg1); |
rcflyair | 0:5e49c3bc05f5 | 76 | }; |
rcflyair | 0:5e49c3bc05f5 | 77 | |
rcflyair | 0:5e49c3bc05f5 | 78 | void SetLEDBar(Arguments *input, Reply *output) |
rcflyair | 0:5e49c3bc05f5 | 79 | { |
rcflyair | 0:5e49c3bc05f5 | 80 | led_green = !led_green; |
rcflyair | 0:5e49c3bc05f5 | 81 | int arg0 = input->getArg<int>(); |
rcflyair | 0:5e49c3bc05f5 | 82 | hc595.bar(arg0); |
rcflyair | 0:5e49c3bc05f5 | 83 | //output->putData(arg0); |
rcflyair | 0:5e49c3bc05f5 | 84 | } |
rcflyair | 0:5e49c3bc05f5 | 85 | |
rcflyair | 0:5e49c3bc05f5 | 86 | void GetParameter(Arguments *input, Reply *output) |
rcflyair | 0:5e49c3bc05f5 | 87 | { |
rcflyair | 0:5e49c3bc05f5 | 88 | led_green = !led_green; |
rcflyair | 0:5e49c3bc05f5 | 89 | //uint8_t arg0 = input->getArg<int>(); |
rcflyair | 0:5e49c3bc05f5 | 90 | //uint32_t temp = motor.GetParameter(arg0); |
rcflyair | 0:5e49c3bc05f5 | 91 | int arg0 = input->getArg<int>(); |
rcflyair | 0:5e49c3bc05f5 | 92 | int temp = motor.GetParameter(arg0); |
rcflyair | 0:5e49c3bc05f5 | 93 | output->putData(temp); |
rcflyair | 0:5e49c3bc05f5 | 94 | } |
rcflyair | 0:5e49c3bc05f5 | 95 | |
rcflyair | 0:5e49c3bc05f5 | 96 | // RPC variable and function assignments |
rcflyair | 0:5e49c3bc05f5 | 97 | RPCFunction rpc_GetStatus(&GetStatus, "GetStatus"); |
rcflyair | 0:5e49c3bc05f5 | 98 | RPCFunction rpc_GetABSPOS(&GetABSPOS, "GetABSPOS"); |
rcflyair | 0:5e49c3bc05f5 | 99 | RPCFunction rpc_Move(&Move, "Move"); |
rcflyair | 0:5e49c3bc05f5 | 100 | RPCFunction rpc_Run(&Run, "Run"); |
rcflyair | 0:5e49c3bc05f5 | 101 | RPCFunction rpc_Stop(&Stop, "Stop"); |
rcflyair | 0:5e49c3bc05f5 | 102 | RPCFunction rpc_ResetPosition(&ResetPosition, "ResetPosition"); |
rcflyair | 0:5e49c3bc05f5 | 103 | RPCFunction rpc_SetParameter(&SetParameter, "SetParameter"); |
rcflyair | 0:5e49c3bc05f5 | 104 | RPCFunction rpc_GetParameter(&GetParameter, "GetParameter"); |
rcflyair | 0:5e49c3bc05f5 | 105 | RPCFunction rpc_SetLEDBar(&SetLEDBar, "SetLEDBar"); |
rcflyair | 0:5e49c3bc05f5 | 106 | |
rcflyair | 0:5e49c3bc05f5 | 107 | int main(){ |
rcflyair | 0:5e49c3bc05f5 | 108 | // Turn off LED's |
rcflyair | 0:5e49c3bc05f5 | 109 | led_red = 1; led_green = 1; led_blue = 1; |
rcflyair | 0:5e49c3bc05f5 | 110 | |
rcflyair | 0:5e49c3bc05f5 | 111 | // set motor parameters |
rcflyair | 0:5e49c3bc05f5 | 112 | motor.Reset(); |
rcflyair | 0:5e49c3bc05f5 | 113 | motor.HardStop(); |
rcflyair | 0:5e49c3bc05f5 | 114 | motor.SetParameter(ACC, 0xFF); |
rcflyair | 0:5e49c3bc05f5 | 115 | motor.SetParameter(DEC, 0xFF); |
rcflyair | 0:5e49c3bc05f5 | 116 | motor.SetParameter(MAX_SPEED, 0x17); |
rcflyair | 0:5e49c3bc05f5 | 117 | motor.SetParameter(MIN_SPEED, 0x00); |
rcflyair | 0:5e49c3bc05f5 | 118 | motor.SetParameter(KVAL_HOLD, 0x29); |
rcflyair | 0:5e49c3bc05f5 | 119 | motor.SetParameter(KVAL_RUN, 0x29); |
rcflyair | 0:5e49c3bc05f5 | 120 | motor.SetParameter(KVAL_ACC, 0x29); |
rcflyair | 0:5e49c3bc05f5 | 121 | motor.SetParameter(KVAL_DEC, 0x29); |
rcflyair | 0:5e49c3bc05f5 | 122 | |
rcflyair | 0:5e49c3bc05f5 | 123 | motor.SetParameter(STALL_TH, 0x30); |
rcflyair | 0:5e49c3bc05f5 | 124 | |
rcflyair | 0:5e49c3bc05f5 | 125 | while(1){ |
rcflyair | 0:5e49c3bc05f5 | 126 | if (sw0) |
rcflyair | 0:5e49c3bc05f5 | 127 | led_red = 0; |
rcflyair | 0:5e49c3bc05f5 | 128 | else |
rcflyair | 0:5e49c3bc05f5 | 129 | led_red = 1; |
rcflyair | 0:5e49c3bc05f5 | 130 | |
rcflyair | 0:5e49c3bc05f5 | 131 | wait_ms(500); |
rcflyair | 0:5e49c3bc05f5 | 132 | } |
rcflyair | 0:5e49c3bc05f5 | 133 | } |