Last active
August 28, 2020 03:17
-
-
Save devyte/ed3fa167d6a3b4296d2e074bdc865074 to your computer and use it in GitHub Desktop.
Multichannel Stepper speed api based on waveform generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stepperapi.h" | |
#include "Arduino.h" | |
#include "core_esp8266_waveform.h" | |
static bool | |
stepperStartCore(int pinPulse, uint32_t analogFreq) | |
{ | |
if (analogFreq == 0) | |
return stopWaveform(pinPulse); //for frequency = 0, stop the pulse train | |
if (analogFreq > 40000) | |
analogFreq = 40000; //clamp to 40KHz at the high end | |
uint32_t analogPeriod = microsecondsToClockCycles(1000000UL) / analogFreq; | |
constexpr uint32_t analogScale = 1023; //set up in setup(), 255 default if not set up | |
uint32_t high = analogPeriod / (2 * analogScale); | |
uint32_t low = analogPeriod - high; | |
pinMode(pinPulse, OUTPUT); | |
if ((low == 0) || (high == 0)) | |
return false; //error | |
return startWaveformClockCycles(pinPulse, high, low, 0); //return whether start succeeded | |
} | |
bool | |
stepperStart(int pinEn, int pinPulse, uint32_t analogFreq) | |
{ | |
//enable stepper | |
pinMode(pinEn, OUTPUT); | |
digitalWrite(pinEn, HIGH); | |
return stepperStartCore(pinPulse, analogFreq); | |
} | |
bool | |
stepperStop(int pinEn, int pinPulse) | |
{ | |
if(!stopWaveform(pinPulse)) | |
return false; | |
//Disable stepper | |
pinMode(pinEn, OUTPUT); | |
digitalWrite(pinEn, LOW); | |
return true; | |
} | |
bool | |
stepperUpdateSpeed(int pinPulse, uint32_t newAnalogFreq) | |
{ | |
//update directly, no need to stop (see analogWrite() function body) | |
return stepperStartCore(pinPulse, newAnalogFreq); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __STEPPERAPI_H__ | |
#define __STEPPERAPI_H__ | |
#include <cstdint> | |
bool | |
stepperStart(int pinEn, int pinPulse, uint32_t analogFreq); | |
bool | |
stepperStop(int pinEn, int pinPulse); | |
bool | |
stepperUpdateSpeed(int pinPulse, uint32_t newAnalogFreq); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __STEPPERCLASS_H__ | |
#define __STEPPERCLASS_H__ | |
#include "stepperapi.h" | |
class Stepper | |
{ | |
private: | |
int pinEn; | |
int pinPulse; | |
bool running; | |
public: | |
Stepper(int pinEn, int pinPulse) | |
: pinEn(pinEn), | |
pinPulse(pinPulse), | |
running(false) | |
{} | |
//disables the pins | |
~Stepper() | |
{ | |
if(running) | |
{ | |
stepperStop(pinEn, pinPulse); | |
releasePins(); | |
} | |
} | |
//Enables the pins for OUTPUT | |
bool start(uint32_t speed) | |
{ | |
if(stepperStart(pinEn, pinPulse, speed)) | |
{ | |
running = true; | |
return true; | |
} | |
return false; | |
} | |
//disables the stepper (En=false) | |
//Does not release the pins, i.e.: they remain in OUTPUT mode | |
bool stop() | |
{ | |
if(stepperStop(pinEn, pinPulse) | |
{ | |
running = false; | |
return true; | |
} | |
return false; | |
} | |
//speed=0 stops the pulse without doing EN=false | |
bool updateSpeed(uint32_t speed) | |
{ | |
return stepperUpdateSpeed(pinPulse, speed); | |
} | |
//Disables the pins, i.e. they go back to INPUT mode and therefore float | |
void releasePins() | |
{ | |
pinMode(pinEn, INPUT); | |
pinMode(pinPulse, INPUT); | |
} | |
bool isRunning() | |
{ | |
return running; | |
} | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino.h" | |
#include "stepperclass.h" | |
namespace | |
{ | |
constexpr int pinEnS1 = 5; | |
constexpr int pinPulseS1 = 4; | |
constexpr int pinEnS2 = 0; | |
constexpr int pinPulseS2 = 2; | |
constexpr int pinEnS3 = 16; | |
constexpr int pinPulseS3 = 13; | |
constexpr int pinAlarm = 12; | |
constexpr int pinLinkageSensor = 14; | |
constexpr int pinRelay = 15; | |
Stepper stepper1(pinEnS1, pinPulseS1); | |
Stepper stepper2(pinEnS2, pinPulseS2); | |
Stepper stepper3(pinEnS3, pinPulseS3); | |
bool startFlag1 = false; | |
bool startFlag2 = false; | |
bool startFlag3 = false; | |
bool stopFlag1 = false; | |
bool stopFlag2 = false; | |
bool stopFlag3 = false; | |
bool updateFlag1 = false; | |
bool updateFlag2 = false; | |
bool updateFlag3 = false; | |
//The unit of these speeds is Hz, i.e.: pulses/second | |
uint32 speed1; | |
uint32 speed2; | |
uint32 speed3; | |
} | |
void | |
handleStepper(Stepper &stepper, bool &startFlag, bool &stopFlag, bool &updateFlag, uint32_t speed) | |
{ | |
if(startFlag) //from ui | |
{ | |
if(!stepper.start(speed)) | |
{ | |
//error starting | |
} | |
startFlag = false; | |
} | |
if(stopFlag) //from ui | |
{ | |
if(!stepper.stop()) | |
{ | |
//error stopping | |
} | |
stopFlag = false; | |
} | |
if(updateFlag) //from ui | |
{ | |
if(!stepper.updateSpeed(speed)) | |
{ | |
//error updating speed | |
} | |
updateFlag = false; | |
} | |
} | |
void | |
setup() | |
{ | |
//set up wifi, webserver, etc | |
} | |
void | |
loop() | |
{ | |
//startFlag, stopFlag, speed would be initially set from a webserver handler | |
//updateFlag and speed would then be set whenever a speed update is available, | |
//e.g.: gps gives new position => calculate speed set point in Hz (speedX = setPointVariableX / 10) => updateFlag = true | |
handleStepper(stepper1, startFlag1, stopFlag1, updateFlag1, speed1); | |
handleStepper(stepper2, startFlag2, stopFlag2, updateFlag2, speed2); | |
handleStepper(stepper3, startFlag3, stopFlag3, updateFlag3, speed3); | |
} |
line 43 in .ino needs another )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
also a typo: F->f
what are the others?