Serial and Steppers - Assignment 4

Dependencies:   mbed

Fork of LCD_Hello_mbed by Takeuchi Kouichi

Committer:
conehead
Date:
Sun Oct 01 06:34:41 2017 +0000
Revision:
1:6df7367a6909
Parent:
0:0a5c73c2369a
V1 of Assignment 4;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
conehead 1:6df7367a6909 1 // Damon Hill - 14234098 - 159.270
conehead 1:6df7367a6909 2 // Assignment 4 - Serial and Steppers
conehead 1:6df7367a6909 3
takeuchi 0:0a5c73c2369a 4 #include "mbed.h"
conehead 1:6df7367a6909 5
conehead 1:6df7367a6909 6 RawSerial pc(PA_2, PA_3); // setup serial pins
conehead 1:6df7367a6909 7
conehead 1:6df7367a6909 8 class Stepper {
conehead 1:6df7367a6909 9 private:
conehead 1:6df7367a6909 10 BusOut stepperMagnets; // stepper pins output
conehead 1:6df7367a6909 11 int delay; // delay between switching magnets (in ms)
conehead 1:6df7367a6909 12 int direction; // direction of motor (1 or 3)
conehead 1:6df7367a6909 13 bool on; // true-motor on, false-motor off
conehead 1:6df7367a6909 14 Ticker t; // ticker timer
conehead 1:6df7367a6909 15 public:
conehead 1:6df7367a6909 16 Stepper(PinName p0, PinName p1, PinName p2, PinName p3, int d):
conehead 1:6df7367a6909 17 stepperMagnets(p0, p1, p2, p3) { // setup stepper pins
conehead 1:6df7367a6909 18 if (d < 4) // set delay
conehead 1:6df7367a6909 19 delay = 4;
conehead 1:6df7367a6909 20 else delay = d;
conehead 1:6df7367a6909 21 direction = 1; // set direction
conehead 1:6df7367a6909 22 on = false;
conehead 1:6df7367a6909 23 }
conehead 1:6df7367a6909 24 void run(void); // declear functions
conehead 1:6df7367a6909 25 void stop(void);
conehead 1:6df7367a6909 26 void setDelay(int d);
conehead 1:6df7367a6909 27 void toggleDirection(void);
conehead 1:6df7367a6909 28 void toggleOn(void);
conehead 1:6df7367a6909 29 };
takeuchi 0:0a5c73c2369a 30
conehead 1:6df7367a6909 31 void Stepper::run(void) {
conehead 1:6df7367a6909 32 static int currentMagnet = 0; // variable that keeps it value when between iterations
conehead 1:6df7367a6909 33 stepperMagnets = (1 << currentMagnet); // shift the currrent pulse
conehead 1:6df7367a6909 34 currentMagnet = (currentMagnet + direction)%4; // get the next pulse depending on direction
conehead 1:6df7367a6909 35 }
conehead 1:6df7367a6909 36
conehead 1:6df7367a6909 37 void Stepper::stop(void) {
conehead 1:6df7367a6909 38 stepperMagnets = 0; // turn all pins off
conehead 1:6df7367a6909 39 }
conehead 1:6df7367a6909 40
conehead 1:6df7367a6909 41 void Stepper::setDelay(int d) {
conehead 1:6df7367a6909 42 if (d < 4) // if the delay is too short
conehead 1:6df7367a6909 43 delay = 4; // set it to the min of 4ms
conehead 1:6df7367a6909 44 else delay = d; // else set delay
conehead 1:6df7367a6909 45 if (on == true) // if the stepper is on
conehead 1:6df7367a6909 46 t.attach_us(this, &Stepper::run, 1000*delay); // start the ticker with the new delay
conehead 1:6df7367a6909 47 }
conehead 1:6df7367a6909 48
conehead 1:6df7367a6909 49 void Stepper::toggleDirection(void) {
conehead 1:6df7367a6909 50 if (direction == 1)
conehead 1:6df7367a6909 51 direction = 3; // shifting by 3 reverses direction
conehead 1:6df7367a6909 52 else direction = 1;
conehead 1:6df7367a6909 53 }
conehead 1:6df7367a6909 54
conehead 1:6df7367a6909 55 void Stepper::toggleOn(void) {
conehead 1:6df7367a6909 56 if (on == false) { // if stepper is not running
conehead 1:6df7367a6909 57 on = true; // turn it on
conehead 1:6df7367a6909 58 t.attach_us(this, &Stepper::run, 1000*delay); // start the ticker with the delay
conehead 1:6df7367a6909 59 }
conehead 1:6df7367a6909 60 else {
conehead 1:6df7367a6909 61 on = false; // turn it off
conehead 1:6df7367a6909 62 t.detach(); // stop the ticker running the motor
conehead 1:6df7367a6909 63 Stepper::stop(); // turn off all magnets
conehead 1:6df7367a6909 64 }
conehead 1:6df7367a6909 65 }
conehead 1:6df7367a6909 66
takeuchi 0:0a5c73c2369a 67
takeuchi 0:0a5c73c2369a 68 int main() {
conehead 1:6df7367a6909 69 Stepper myStepper(D10, D9, D7, D6, 4); // setup stepper pins
conehead 1:6df7367a6909 70 char serialIn[5]; // serial buffer
conehead 1:6df7367a6909 71 unsigned int stringLen = 0; // length of buffer
conehead 1:6df7367a6909 72
conehead 1:6df7367a6909 73 pc.puts("===[Serial Stepper Control]===\r\n"); // serial intro
conehead 1:6df7367a6909 74 pc.puts("s-ON/OFF\n");
conehead 1:6df7367a6909 75 pc.puts("t-change direction\n");
conehead 1:6df7367a6909 76 pc.puts("d-set delay\n");
conehead 1:6df7367a6909 77
conehead 1:6df7367a6909 78 while (1) {
conehead 1:6df7367a6909 79 if (pc.readable()) { // if something is inputted via serial
conehead 1:6df7367a6909 80 char c = pc.getc(); // get the char
conehead 1:6df7367a6909 81 pc.putc(c); // echo it back
conehead 1:6df7367a6909 82 serialIn[stringLen] = c; // store it in the buffer
conehead 1:6df7367a6909 83 stringLen ++; // increment buffer length
conehead 1:6df7367a6909 84 if (c == '\n' or stringLen == 5) { // if a command has been entered
conehead 1:6df7367a6909 85 if (serialIn[0] == 's') {
conehead 1:6df7367a6909 86 pc.puts("Stepper toggled\n"); // toggle stepper on and off
conehead 1:6df7367a6909 87 myStepper.toggleOn();
conehead 1:6df7367a6909 88 }
conehead 1:6df7367a6909 89 else if (serialIn[0] == 't') {
conehead 1:6df7367a6909 90 pc.puts("Direction change\n"); // change stepper direction
conehead 1:6df7367a6909 91 myStepper.toggleDirection();
conehead 1:6df7367a6909 92 }
conehead 1:6df7367a6909 93 else if (serialIn[0] == 'd') {
conehead 1:6df7367a6909 94 pc.puts("Delay set\n"); // set delay
conehead 1:6df7367a6909 95 if (stringLen == 4) { // if delay is less than 10
conehead 1:6df7367a6909 96 myStepper.setDelay(serialIn[2]-'0'); // do this to change char to int
conehead 1:6df7367a6909 97 }
conehead 1:6df7367a6909 98 else { // if delay is 10 or greater
conehead 1:6df7367a6909 99 myStepper.setDelay(((serialIn[2]-'0')*10)+(serialIn[3]-'0')); // do this to change char to int
conehead 1:6df7367a6909 100 }
conehead 1:6df7367a6909 101 }
conehead 1:6df7367a6909 102 else {
conehead 1:6df7367a6909 103 pc.puts("Invalid command\n");
conehead 1:6df7367a6909 104 }
conehead 1:6df7367a6909 105 stringLen = 0; // reset buffer
conehead 1:6df7367a6909 106 }
conehead 1:6df7367a6909 107 }
conehead 1:6df7367a6909 108 }
takeuchi 0:0a5c73c2369a 109 }