Class library for a Stepper Motor to be able to Select amount of steps,set the time interval between steps, run stepper motor freely and stop the stepper motor.

Committer:
Armand
Date:
Thu Mar 09 06:31:16 2017 +0000
Revision:
3:82951e628d6a
Parent:
2:bfdd7246ac5e
Just added email address

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Armand 0:b0c267f9cc05 1 #include "Stepper.h"
Armand 0:b0c267f9cc05 2 #include "mbed.h"
Armand 0:b0c267f9cc05 3
Armand 0:b0c267f9cc05 4 Stepper::Stepper(PinName Coil1, PinName Coil2, PinName Coil3, PinName Coil4) : _Coils(Coil1, Coil2, Coil3, Coil4)
Armand 0:b0c267f9cc05 5 {
Armand 0:b0c267f9cc05 6 _Coils = 0;
Armand 0:b0c267f9cc05 7 stepcount = 0;
Armand 0:b0c267f9cc05 8 Prevtime = 0;
Armand 0:b0c267f9cc05 9 secinter = 0.5;
Armand 0:b0c267f9cc05 10 lastsecinter = 0;
Armand 0:b0c267f9cc05 11 StepDir = 1;
Armand 0:b0c267f9cc05 12 stepping = 0;
Armand 0:b0c267f9cc05 13 stepincr = 0;
Armand 0:b0c267f9cc05 14 stepper_steps[0] = 0x09;
Armand 0:b0c267f9cc05 15 stepper_steps[1] = 0x0A;
Armand 0:b0c267f9cc05 16 stepper_steps[2] = 0x06;
Armand 0:b0c267f9cc05 17 stepper_steps[3] = 0x05;
Armand 0:b0c267f9cc05 18
Armand 0:b0c267f9cc05 19 }
Armand 0:b0c267f9cc05 20
Armand 0:b0c267f9cc05 21 void Stepper::Run()
Armand 0:b0c267f9cc05 22 {
Armand 0:b0c267f9cc05 23
Armand 0:b0c267f9cc05 24 run = 1;
Armand 0:b0c267f9cc05 25 stepping = 0;
Armand 0:b0c267f9cc05 26
Armand 0:b0c267f9cc05 27 if(secinter != lastsecinter)
Armand 0:b0c267f9cc05 28 {
Armand 0:b0c267f9cc05 29 ticker.attach(this, &Stepper::stepper_isr,secinter);
Armand 0:b0c267f9cc05 30 lastsecinter = secinter;
Armand 0:b0c267f9cc05 31 }
Armand 0:b0c267f9cc05 32 }
Armand 0:b0c267f9cc05 33
Armand 0:b0c267f9cc05 34 void Stepper::Stop()
Armand 0:b0c267f9cc05 35 {
Armand 0:b0c267f9cc05 36 run = 0;
Armand 0:b0c267f9cc05 37 stepping = 0;
Armand 0:b0c267f9cc05 38
Armand 0:b0c267f9cc05 39
Armand 0:b0c267f9cc05 40 }
Armand 0:b0c267f9cc05 41
Armand 2:bfdd7246ac5e 42 void Stepper::Direction(bool dir)
Armand 0:b0c267f9cc05 43 {
Armand 2:bfdd7246ac5e 44 if(dir == true)
Armand 0:b0c267f9cc05 45 {
Armand 0:b0c267f9cc05 46 StepDir = 1;
Armand 0:b0c267f9cc05 47 }
Armand 2:bfdd7246ac5e 48 else if(dir == false)
Armand 0:b0c267f9cc05 49 {
Armand 0:b0c267f9cc05 50 StepDir = -1;
Armand 0:b0c267f9cc05 51 }
Armand 0:b0c267f9cc05 52 }
Armand 0:b0c267f9cc05 53
Armand 0:b0c267f9cc05 54 void Stepper::Interval(float sec)
Armand 0:b0c267f9cc05 55 {
Armand 0:b0c267f9cc05 56 secinter = sec;
Armand 0:b0c267f9cc05 57 if(secinter != lastsecinter)
Armand 0:b0c267f9cc05 58 {
Armand 0:b0c267f9cc05 59 ticker.attach(this, &Stepper::stepper_isr,secinter);
Armand 0:b0c267f9cc05 60 lastsecinter = secinter;
Armand 0:b0c267f9cc05 61 }
Armand 0:b0c267f9cc05 62 }
Armand 0:b0c267f9cc05 63
Armand 0:b0c267f9cc05 64 void Stepper::Step(int steps)
Armand 0:b0c267f9cc05 65 {
Armand 0:b0c267f9cc05 66 Steps = steps;
Armand 0:b0c267f9cc05 67 stepincr = 0;
Armand 0:b0c267f9cc05 68 run = 0;
Armand 0:b0c267f9cc05 69 stepping = 1;
Armand 0:b0c267f9cc05 70
Armand 0:b0c267f9cc05 71 if(secinter != lastsecinter)
Armand 0:b0c267f9cc05 72 {
Armand 0:b0c267f9cc05 73 ticker.attach(this, &Stepper::stepper_isr,secinter);
Armand 0:b0c267f9cc05 74 lastsecinter = secinter;
Armand 0:b0c267f9cc05 75 }
Armand 0:b0c267f9cc05 76
Armand 0:b0c267f9cc05 77
Armand 0:b0c267f9cc05 78 }
Armand 0:b0c267f9cc05 79
Armand 0:b0c267f9cc05 80 void Stepper::stepper_isr()
Armand 0:b0c267f9cc05 81 {
Armand 0:b0c267f9cc05 82 if(run == 1)
Armand 0:b0c267f9cc05 83 {
Armand 0:b0c267f9cc05 84 _Coils = stepper_steps[stepcount];
Armand 0:b0c267f9cc05 85
Armand 0:b0c267f9cc05 86 stepcount = stepcount + StepDir;
Armand 0:b0c267f9cc05 87
Armand 0:b0c267f9cc05 88 if(stepcount > 3)
Armand 0:b0c267f9cc05 89 {
Armand 0:b0c267f9cc05 90 stepcount = 0;
Armand 0:b0c267f9cc05 91 }
Armand 0:b0c267f9cc05 92 if(stepcount < 0)
Armand 0:b0c267f9cc05 93 {
Armand 0:b0c267f9cc05 94 stepcount = 3;
Armand 0:b0c267f9cc05 95 }
Armand 0:b0c267f9cc05 96
Armand 0:b0c267f9cc05 97 }
Armand 0:b0c267f9cc05 98
Armand 0:b0c267f9cc05 99 if(stepping == 1 && stepincr < Steps)
Armand 0:b0c267f9cc05 100 {
Armand 0:b0c267f9cc05 101 _Coils = stepper_steps[stepcount];
Armand 0:b0c267f9cc05 102
Armand 0:b0c267f9cc05 103 stepcount = stepcount + StepDir;
Armand 0:b0c267f9cc05 104 stepincr++;
Armand 0:b0c267f9cc05 105
Armand 0:b0c267f9cc05 106 if(stepcount > 3)
Armand 0:b0c267f9cc05 107 {
Armand 0:b0c267f9cc05 108 stepcount = 0;
Armand 0:b0c267f9cc05 109 }
Armand 0:b0c267f9cc05 110 if(stepcount < 0)
Armand 0:b0c267f9cc05 111 {
Armand 0:b0c267f9cc05 112 stepcount = 3;
Armand 0:b0c267f9cc05 113 }
Armand 0:b0c267f9cc05 114
Armand 0:b0c267f9cc05 115 }
Armand 0:b0c267f9cc05 116
Armand 0:b0c267f9cc05 117
Armand 0:b0c267f9cc05 118
Armand 0:b0c267f9cc05 119 }
Armand 0:b0c267f9cc05 120
Armand 0:b0c267f9cc05 121