conehead NZ / Mbed 2 deprecated Ass4

Dependencies:   mbed

Fork of LCD_Hello_mbed by Takeuchi Kouichi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Damon Hill - 14234098 - 159.270
00002 // Assignment 4 - Serial and Steppers
00003 
00004 #include "mbed.h"
00005 
00006 RawSerial pc(PA_2, PA_3);       // setup serial pins
00007 
00008 class Stepper {
00009 private:
00010     BusOut stepperMagnets;      // stepper pins output  
00011     int delay;          // delay between switching magnets (in ms)
00012     int direction;      // direction of motor (1 or 3)
00013     bool on;            // true-motor on, false-motor off
00014     Ticker t;           // ticker timer
00015 public:
00016     Stepper(PinName p0, PinName p1, PinName p2, PinName p3, int d):
00017         stepperMagnets(p0, p1, p2, p3) {        // setup stepper pins
00018         if (d < 4)      // set delay
00019             delay = 4;
00020         else delay = d;
00021         direction = 1;      // set direction
00022         on = false;      
00023     }
00024     void run(void);     // declear functions
00025     void stop(void);
00026     void setDelay(int d);
00027     void toggleDirection(void);
00028     void toggleOn(void);
00029 };
00030 
00031 void Stepper::run(void) {
00032     static int currentMagnet = 0;       // variable that keeps it value when between iterations 
00033             stepperMagnets = (1 << currentMagnet);      // shift the currrent pulse
00034             currentMagnet = (currentMagnet + direction)%4;      // get the next pulse depending on direction
00035 }
00036 
00037 void Stepper::stop(void) {
00038     stepperMagnets = 0;     // turn all pins off
00039 }
00040 
00041 void Stepper::setDelay(int d) {
00042     if (d < 4)      // if the delay is too short
00043         delay = 4;      // set it to the min of 4ms
00044     else delay = d;     // else set delay
00045     if (on == true)     // if the stepper is on
00046         t.attach_us(this, &Stepper::run, 1000*delay);       // start the ticker with the new delay   
00047 }
00048 
00049 void Stepper::toggleDirection(void) {
00050     if (direction == 1)
00051         direction = 3;      // shifting by 3 reverses direction
00052     else direction = 1;
00053 }
00054 
00055 void Stepper::toggleOn(void) {
00056     if (on == false) {      // if stepper is not running 
00057         on = true;      // turn it on
00058         t.attach_us(this, &Stepper::run, 1000*delay);       // start the ticker with the delay 
00059     }
00060     else {
00061         on = false;     // turn it off
00062         t.detach();     // stop the ticker running the motor
00063         Stepper::stop();        // turn off all magnets
00064     }
00065 }
00066     
00067 
00068 int main() {
00069     Stepper myStepper(D10, D9, D7, D6, 4);      // setup stepper pins
00070     char serialIn[5];       // serial buffer
00071     unsigned int stringLen = 0;     // length of buffer
00072     
00073     pc.puts("===[Serial Stepper Control]===\r\n");      // serial intro 
00074     pc.puts("s-ON/OFF\n");
00075     pc.puts("t-change direction\n");
00076     pc.puts("d-set delay\n");
00077     
00078     while (1) {
00079         if (pc.readable()) {        // if something is inputted via serial
00080             char c = pc.getc();     // get the char
00081             pc.putc(c);     // echo it back
00082             serialIn[stringLen] = c;        // store it in the buffer
00083             stringLen ++;       // increment buffer length
00084             if (c == '\n' or stringLen == 5) {      // if a command has been entered
00085                 if (serialIn[0] == 's') {      
00086                     pc.puts("Stepper toggled\n");       // toggle stepper on and off
00087                     myStepper.toggleOn();
00088                 }
00089                 else if (serialIn[0] == 't') {
00090                     pc.puts("Direction change\n");      // change stepper direction
00091                     myStepper.toggleDirection();
00092                 }
00093                 else if (serialIn[0] == 'd') {
00094                     pc.puts("Delay set\n");     // set delay
00095                     if (stringLen == 4) {       // if delay is less than 10
00096                         myStepper.setDelay(serialIn[2]-'0');    // do this to change char to int
00097                     }
00098                     else {      // if delay is 10 or greater
00099                         myStepper.setDelay(((serialIn[2]-'0')*10)+(serialIn[3]-'0'));        // do this to change char to int
00100                     }
00101                 }
00102                 else {
00103                     pc.puts("Invalid command\n");
00104                 }
00105                 stringLen = 0; // reset buffer
00106             }
00107         }
00108     }
00109 }