1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
#include <SPI.h> #include <HighPowerStepperDriver.h> #include "Arduino.h" #include <Servo.h> #include <digitalWriteFast.h>
const uint8_t DirPin = 8; const uint8_t StepPin = 9; const uint8_t CSPin = 10;
const uint8_t SleepPin = 7; const uint8_t RstPin = 6; const uint8_t FaultPin = 5;
const uint16_t StepPeriodUs = 1000;
char ch; float DutyCircle; const byte numChars = 32; char comdata[numChars]; int motorState;
boolean newData = false;
HighPowerStepperDriver sd;
#define zChannel 4 #define EncoderPinA 2 #define EncoderPinB 3 #define EncoderIsReversed volatile bool EncoderBSet; volatile long EncoderTicks = 0; unsigned int flagA = 0; unsigned int flagB = 0;
void setup() { Serial.begin(115200); SPI.begin(); sd.setChipSelectPin(CSPin);
pinMode(StepPin, OUTPUT); digitalWrite(StepPin, LOW); pinMode(DirPin, OUTPUT); digitalWrite(DirPin, LOW);
pinMode(SleepPin, OUTPUT); digitalWrite(SleepPin, HIGH); pinMode(RstPin, OUTPUT); digitalWrite(RstPin, LOW);
pinMode(FaultPin, INPUT);
delay(10);
sd.resetSettings(); sd.clearStatus(); sd.setDecayMode(HPSDDecayMode::AutoMixed);
sd.setCurrentMilliamps36v4(600);
sd.setStepMode(HPSDStepMode::MicroStep16);
sd.enableDriver();
pinMode(EncoderPinA, INPUT); digitalWrite(EncoderPinA, LOW); pinMode(EncoderPinB, INPUT); digitalWrite(EncoderPinB, LOW); pinMode(zChannel,INPUT); attachInterrupt(digitalPinToInterrupt(EncoderPinA), HandleInterruptA, RISING); }
void loop() { checkStatus(); while (!Serial) { ; }
if (Serial.available() > 0) { newData = false; recvWithStartEndMarkers(); if (newData == true){
if (comdata[0] == '+' || comdata[0] == '-' || comdata[0] == 'A' || comdata[0] == 'E'){ ch = comdata[0]; char* numStart = &comdata[1]; char* endptr; long stepp = strtol(numStart, &endptr, 10); if (numStart == endptr) { Serial.println("Error: No digits were found after the sign. Please re-enter the command."); return; } else if (*endptr != '\0') { Serial.println("Error: Extra characters after number. Please re-enter the command."); return; } else if (comdata[0] == '+' || comdata[0] == '-') { DoSerial(ch, stepp); } else if (comdata[0] == 'A') { if (stepp > 1000 || stepp < 200) { Serial.println("Over/Below Current"); return; } sd.setCurrentMilliamps36v4(stepp); Serial.print("The motor current is set to be "); Serial.print(stepp); Serial.println(" mA"); } else if (comdata[0] == 'E') { if (digitalReadFast(zChannel)){ Serial.println("High"); delay(100); } Serial.print("number of pulses: "); Serial.println(EncoderTicks); } } else { Serial.println("Error: Command must start with '+', '-', 'A', or 'E'. Please re-enter the command."); return; } newData = false; }
} }
void step() { digitalWrite(StepPin, HIGH); delayMicroseconds(3); digitalWrite(StepPin, LOW); delayMicroseconds(3); }
void setDirection(bool dir) { delayMicroseconds(1); digitalWrite(DirPin, dir); delayMicroseconds(1); }
void DoSerial(char ch, long stepp) {
switch (ch) { case '+': setDirection(0); for(unsigned int x = 0; x < stepp; x++) { step(); delayMicroseconds(StepPeriodUs); checkStatus(); } break; case '-': setDirection(1); for(unsigned int x = 0; x < stepp; x++) { step(); delayMicroseconds(StepPeriodUs); checkStatus(); } break; default: break; } }
void recvWithStartEndMarkers() { static boolean recvInProgress = false; static byte ndx = 0; char startMarker = '<'; char endMarker = '>'; char rc; while (Serial.available() > 0 && newData == false) { rc = Serial.read(); delay(2); if (recvInProgress == true) { if (rc != endMarker) { comdata[ndx] = rc; ndx++; if (ndx >= numChars) { ndx = numChars - 1; } } else { comdata[ndx] = '\0'; recvInProgress = false; ndx = 0; newData = true; } }
else if (rc == startMarker) { recvInProgress = true; } } }
void HandleInterruptA() { EncoderBSet = digitalReadFast(EncoderPinB); EncoderTicks -= EncoderBSet ? -1 : +1; char i; i = digitalRead(EncoderPinB); if (i == 1) flagA += 1; else flagB += 1; } void checkStatus() { motorState = digitalRead(FaultPin); delay(2); if (motorState == 0){ Serial.println("Faulty"); delay(2); digitalWrite(SleepPin, LOW); } }
|