Generic Step Motor WebInterface - control a step motor using a Pololu A4983 driver from a webinterface (EXPERIMENTAL PROTOTYPE - just to be used as a proof-of-concept for a IoT talk, will not be updating this code so often)

Dependencies:   EthernetNetIf RPCInterface mbed HTTPServer

Committer:
botdream
Date:
Mon Apr 16 09:41:53 2012 +0000
Revision:
0:8b3857d4ce02

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
botdream 0:8b3857d4ce02 1 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 2 #include "mbed.h"
botdream 0:8b3857d4ce02 3 #include "A4983.h"
botdream 0:8b3857d4ce02 4 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 5 // Pins configuration
botdream 0:8b3857d4ce02 6 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 7 //LEDs port definition on mbed
botdream 0:8b3857d4ce02 8 DigitalOut myled1(LED1);
botdream 0:8b3857d4ce02 9 DigitalOut myled2(LED2);
botdream 0:8b3857d4ce02 10 DigitalOut myled3(LED3);
botdream 0:8b3857d4ce02 11 DigitalOut myled4(LED4);
botdream 0:8b3857d4ce02 12
botdream 0:8b3857d4ce02 13 //ports connected to pololu driver board = straightforward connection of one side of mbed
botdream 0:8b3857d4ce02 14 DigitalOut Dir(p23); //direction of movement
botdream 0:8b3857d4ce02 15 DigitalOut Step(p24); //make one step at rising edge
botdream 0:8b3857d4ce02 16 DigitalOut nSleep(p25); // be HIGH to make it work
botdream 0:8b3857d4ce02 17 DigitalOut nReset(p26); // be HIGH to make it work
botdream 0:8b3857d4ce02 18 DigitalOut MS3(p19); //microstep mode selectors
botdream 0:8b3857d4ce02 19 DigitalOut MS2(p18);
botdream 0:8b3857d4ce02 20 DigitalOut MS1(p17);
botdream 0:8b3857d4ce02 21 DigitalOut nEnable(p27);// be LOW to make it work
botdream 0:8b3857d4ce02 22
botdream 0:8b3857d4ce02 23 #define HIGH 1
botdream 0:8b3857d4ce02 24 #define LOW 0
botdream 0:8b3857d4ce02 25 #define ON 1
botdream 0:8b3857d4ce02 26 #define OFF 0
botdream 0:8b3857d4ce02 27 #define Clockwise LOW
botdream 0:8b3857d4ce02 28 #define CounterClockwise HIGH
botdream 0:8b3857d4ce02 29
botdream 0:8b3857d4ce02 30 //which mode of driver to be used. This is now 1/16 stepping.
botdream 0:8b3857d4ce02 31 #define Factor 16
botdream 0:8b3857d4ce02 32 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 33 // USB serial port setup - debug messages
botdream 0:8b3857d4ce02 34 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 35 Serial pc(USBTX, USBRX); // tx, rx
botdream 0:8b3857d4ce02 36 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 37
botdream 0:8b3857d4ce02 38 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 39 // constructor
botdream 0:8b3857d4ce02 40 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 41 A4983::A4983()
botdream 0:8b3857d4ce02 42 {
botdream 0:8b3857d4ce02 43 //set communication speed
botdream 0:8b3857d4ce02 44 pc.baud(115200);
botdream 0:8b3857d4ce02 45 //test to see if comm is working
botdream 0:8b3857d4ce02 46 printf("\r\nInitialize MBED Serial Port!\r\n");
botdream 0:8b3857d4ce02 47
botdream 0:8b3857d4ce02 48 myled1 = 0;
botdream 0:8b3857d4ce02 49 myled2 = 0;
botdream 0:8b3857d4ce02 50 myled3 = 0;
botdream 0:8b3857d4ce02 51 myled4 = 0;
botdream 0:8b3857d4ce02 52
botdream 0:8b3857d4ce02 53 //-------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 54 // adjust microstepping mode
botdream 0:8b3857d4ce02 55 adjust_microstepping_mode(16);
botdream 0:8b3857d4ce02 56
botdream 0:8b3857d4ce02 57 // initialisation of power stage
botdream 0:8b3857d4ce02 58 Dir = CounterClockwise;
botdream 0:8b3857d4ce02 59 Step = LOW;
botdream 0:8b3857d4ce02 60 nSleep = HIGH;
botdream 0:8b3857d4ce02 61 nEnable = HIGH;
botdream 0:8b3857d4ce02 62 nReset = LOW;
botdream 0:8b3857d4ce02 63 myled1 = 1;
botdream 0:8b3857d4ce02 64 myled2 = 0;
botdream 0:8b3857d4ce02 65 myled3 = 0;
botdream 0:8b3857d4ce02 66 myled4 = 0;
botdream 0:8b3857d4ce02 67
botdream 0:8b3857d4ce02 68 wait(0.5);
botdream 0:8b3857d4ce02 69
botdream 0:8b3857d4ce02 70 nReset = HIGH;
botdream 0:8b3857d4ce02 71 myled1 = 0;
botdream 0:8b3857d4ce02 72 myled2 = 1;
botdream 0:8b3857d4ce02 73 myled3 = 0;
botdream 0:8b3857d4ce02 74 myled4 = 0;
botdream 0:8b3857d4ce02 75
botdream 0:8b3857d4ce02 76 wait(0.5);
botdream 0:8b3857d4ce02 77
botdream 0:8b3857d4ce02 78 nEnable = LOW;
botdream 0:8b3857d4ce02 79 myled1 = 1;
botdream 0:8b3857d4ce02 80 myled2 = 1;
botdream 0:8b3857d4ce02 81 myled3 = 0;
botdream 0:8b3857d4ce02 82 myled4 = 0;
botdream 0:8b3857d4ce02 83
botdream 0:8b3857d4ce02 84 wait(0.5);
botdream 0:8b3857d4ce02 85
botdream 0:8b3857d4ce02 86 Step = HIGH;
botdream 0:8b3857d4ce02 87 myled1 = 0;
botdream 0:8b3857d4ce02 88 myled2 = 0;
botdream 0:8b3857d4ce02 89 myled3 = 1;
botdream 0:8b3857d4ce02 90 myled4 = 0;
botdream 0:8b3857d4ce02 91
botdream 0:8b3857d4ce02 92 wait(0.5);
botdream 0:8b3857d4ce02 93
botdream 0:8b3857d4ce02 94 Step = LOW;
botdream 0:8b3857d4ce02 95 myled1 = 1;
botdream 0:8b3857d4ce02 96 myled2 = 0;
botdream 0:8b3857d4ce02 97 myled3 = 1;
botdream 0:8b3857d4ce02 98 myled4 = 0;
botdream 0:8b3857d4ce02 99
botdream 0:8b3857d4ce02 100 wait(0.5);
botdream 0:8b3857d4ce02 101 //-------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 102
botdream 0:8b3857d4ce02 103 f_motor_enable = 0; // flag to Enable/Disable Step Motor, 0 => Disable, 1 => Enable
botdream 0:8b3857d4ce02 104 f_motor_direction = 0; // flag for the Step Motor direction, 0 => Normal, 1 => Inverted
botdream 0:8b3857d4ce02 105 k_delay = 0.001; // delay constant in seconds (Step Motor Speed)
botdream 0:8b3857d4ce02 106 }
botdream 0:8b3857d4ce02 107 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 108 void A4983::adjust_microstepping_mode(uint8_t factor)
botdream 0:8b3857d4ce02 109 {
botdream 0:8b3857d4ce02 110 if(factor==1)
botdream 0:8b3857d4ce02 111 {
botdream 0:8b3857d4ce02 112 MS1 = LOW;
botdream 0:8b3857d4ce02 113 MS2 = LOW;
botdream 0:8b3857d4ce02 114 MS3 = LOW; // 1/1
botdream 0:8b3857d4ce02 115 }
botdream 0:8b3857d4ce02 116 else if(factor==2)
botdream 0:8b3857d4ce02 117 {
botdream 0:8b3857d4ce02 118 MS1 = HIGH;
botdream 0:8b3857d4ce02 119 MS2 = LOW;
botdream 0:8b3857d4ce02 120 MS3 = LOW; // 1/2
botdream 0:8b3857d4ce02 121 }
botdream 0:8b3857d4ce02 122 else if(factor==4)
botdream 0:8b3857d4ce02 123 {
botdream 0:8b3857d4ce02 124 MS1 = LOW;
botdream 0:8b3857d4ce02 125 MS2 = HIGH;
botdream 0:8b3857d4ce02 126 MS3 = LOW; // 1/4
botdream 0:8b3857d4ce02 127 }
botdream 0:8b3857d4ce02 128 else if(factor==8)
botdream 0:8b3857d4ce02 129 {
botdream 0:8b3857d4ce02 130 MS1 = HIGH;
botdream 0:8b3857d4ce02 131 MS2 = HIGH;
botdream 0:8b3857d4ce02 132 MS3 = LOW; // 1/8
botdream 0:8b3857d4ce02 133 }
botdream 0:8b3857d4ce02 134 else if(factor==16)
botdream 0:8b3857d4ce02 135 {
botdream 0:8b3857d4ce02 136 MS1 = HIGH;
botdream 0:8b3857d4ce02 137 MS2 = HIGH;
botdream 0:8b3857d4ce02 138 MS3 = HIGH; // 1/16
botdream 0:8b3857d4ce02 139 }
botdream 0:8b3857d4ce02 140 }
botdream 0:8b3857d4ce02 141 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 142 // Step function
botdream 0:8b3857d4ce02 143 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 144 void A4983::singlestep()
botdream 0:8b3857d4ce02 145 {
botdream 0:8b3857d4ce02 146 if(Dir != f_motor_direction)
botdream 0:8b3857d4ce02 147 Dir = f_motor_direction;
botdream 0:8b3857d4ce02 148
botdream 0:8b3857d4ce02 149 Step = HIGH;
botdream 0:8b3857d4ce02 150 wait(k_delay/2);
botdream 0:8b3857d4ce02 151
botdream 0:8b3857d4ce02 152 Step = LOW;
botdream 0:8b3857d4ce02 153 wait(k_delay/2);
botdream 0:8b3857d4ce02 154 }
botdream 0:8b3857d4ce02 155 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 156 // Release Motor Command
botdream 0:8b3857d4ce02 157 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 158 void A4983::sleep(bool value)
botdream 0:8b3857d4ce02 159 {
botdream 0:8b3857d4ce02 160 if(value == true)
botdream 0:8b3857d4ce02 161 {
botdream 0:8b3857d4ce02 162 //nEnable = LOW;
botdream 0:8b3857d4ce02 163 nSleep = LOW;
botdream 0:8b3857d4ce02 164 }
botdream 0:8b3857d4ce02 165 else
botdream 0:8b3857d4ce02 166 {
botdream 0:8b3857d4ce02 167 //nEnable = HIGH;
botdream 0:8b3857d4ce02 168 nSleep = HIGH;
botdream 0:8b3857d4ce02 169 }
botdream 0:8b3857d4ce02 170 }
botdream 0:8b3857d4ce02 171 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 172 // check if f_motor_enable is true and call singlestep() - loop mode
botdream 0:8b3857d4ce02 173 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 174 void A4983::looprun()
botdream 0:8b3857d4ce02 175 {
botdream 0:8b3857d4ce02 176 if(f_motor_enable == 1)
botdream 0:8b3857d4ce02 177 {
botdream 0:8b3857d4ce02 178 singlestep();
botdream 0:8b3857d4ce02 179 }
botdream 0:8b3857d4ce02 180 }
botdream 0:8b3857d4ce02 181 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 182 // enable loop mode
botdream 0:8b3857d4ce02 183 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 184 void A4983::loopstart()
botdream 0:8b3857d4ce02 185 {
botdream 0:8b3857d4ce02 186 f_motor_enable = 1;
botdream 0:8b3857d4ce02 187 }
botdream 0:8b3857d4ce02 188 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 189 // disable loop mode
botdream 0:8b3857d4ce02 190 //---------------------------------------------------------------------------------------------
botdream 0:8b3857d4ce02 191 void A4983::loopstop()
botdream 0:8b3857d4ce02 192 {
botdream 0:8b3857d4ce02 193 f_motor_enable = 0;
botdream 0:8b3857d4ce02 194 }
botdream 0:8b3857d4ce02 195 //---------------------------------------------------------------------------------------------