Skip to content

Commit

Permalink
esp32: firmware: format code using clangd
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylensky committed Dec 15, 2024
1 parent c131647 commit db39777
Show file tree
Hide file tree
Showing 11 changed files with 849 additions and 737 deletions.
209 changes: 113 additions & 96 deletions esp32_wireless_control/firmware/axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,126 +6,143 @@ Axis dec_axis(2, AXIS2_DIR, DEC_INVERT_DIR_PIN);
volatile bool ra_axis_step_phase = 0;
volatile bool dec_axis_step_phase = 0;

void IRAM_ATTR stepTimerRA_ISR() {
//ra ISR
ra_axis_step_phase = !ra_axis_step_phase;
digitalWrite(AXIS1_STEP, ra_axis_step_phase); //toggle step pin at required frequency
if (ra_axis.counterActive && ra_axis_step_phase) { //if counter active
int temp = ra_axis.axisCountValue;
ra_axis.axisAbsoluteDirection ? temp++ : temp--;
ra_axis.axisCountValue = temp;
if (ra_axis.goToTarget && ra_axis.axisCountValue == ra_axis.targetCount) {
ra_axis.goToTarget = false;
ra_axis.stopSlew();
void IRAM_ATTR stepTimerRA_ISR()
{
// ra ISR
ra_axis_step_phase = !ra_axis_step_phase;
digitalWrite(AXIS1_STEP,
ra_axis_step_phase); // toggle step pin at required frequency
if (ra_axis.counterActive && ra_axis_step_phase)
{ // if counter active
int temp = ra_axis.axisCountValue;
ra_axis.axisAbsoluteDirection ? temp++ : temp--;
ra_axis.axisCountValue = temp;
if (ra_axis.goToTarget && ra_axis.axisCountValue == ra_axis.targetCount)
{
ra_axis.goToTarget = false;
ra_axis.stopSlew();
}
}
}
}

void IRAM_ATTR stepTimerDEC_ISR() {
//dec ISR
dec_axis_step_phase = !dec_axis_step_phase;
digitalWrite(AXIS2_STEP, dec_axis_step_phase); //toggle step pin at required frequency
if (dec_axis_step_phase && dec_axis.counterActive) { //if counter active
int temp = dec_axis.axisCountValue;
dec_axis.axisAbsoluteDirection ? temp++ : temp--;
dec_axis.axisCountValue = temp;
}
void IRAM_ATTR stepTimerDEC_ISR()
{
// dec ISR
dec_axis_step_phase = !dec_axis_step_phase;
digitalWrite(AXIS2_STEP,
dec_axis_step_phase); // toggle step pin at required frequency
if (dec_axis_step_phase && dec_axis.counterActive)
{ // if counter active
int temp = dec_axis.axisCountValue;
dec_axis.axisAbsoluteDirection ? temp++ : temp--;
dec_axis.axisCountValue = temp;
}
}

void IRAM_ATTR slewTimeOutTimer_ISR() {
ra_axis.stopSlew();
void IRAM_ATTR slewTimeOutTimer_ISR()
{
ra_axis.stopSlew();
}

HardwareTimer slewTimeOut(2000, &slewTimeOutTimer_ISR);

Axis::Axis(uint8_t axis, uint8_t dirPinforAxis, bool invertDirPin) : stepTimer(40000000)
{
axisNumber = axis;
trackingDirection = c_DIRECTION;
dirPin = dirPinforAxis;
invertDirectionPin = invertDirPin;
trackingRate = TRACKING_SIDEREAL;
switch (axisNumber)
{
case 1:
stepTimer.attachInterupt(&stepTimerRA_ISR);
break;
case 2:
stepTimer.attachInterupt(&stepTimerDEC_ISR);
break;
}

Axis::Axis(uint8_t axis, uint8_t dirPinforAxis, bool invertDirPin)
: stepTimer(40000000) {
axisNumber = axis;
trackingDirection = c_DIRECTION;
dirPin = dirPinforAxis;
invertDirectionPin = invertDirPin;
trackingRate = TRACKING_SIDEREAL;
switch (axisNumber) {
case 1:
stepTimer.attachInterupt(&stepTimerRA_ISR);
break;
case 2:
stepTimer.attachInterupt(&stepTimerDEC_ISR);
break;
}

if (DEFAULT_ENABLE_TRACKING == 1 && axisNumber == 1) {
startTracking(TRACKING_SIDEREAL, trackingDirection);
}
if (DEFAULT_ENABLE_TRACKING == 1 && axisNumber == 1)
{
startTracking(TRACKING_SIDEREAL, trackingDirection);
}
}


void Axis::startTracking(trackingRateS rate, bool directionArg) {
trackingRate = rate;
trackingDirection = directionArg;
axisAbsoluteDirection = directionArg;
setDirection(axisAbsoluteDirection);
trackingActive = true;
stepTimer.stop();
setMicrostep(16);
stepTimer.start(trackingRate, true);

void Axis::startTracking(trackingRateS rate, bool directionArg)
{
trackingRate = rate;
trackingDirection = directionArg;
axisAbsoluteDirection = directionArg;
setDirection(axisAbsoluteDirection);
trackingActive = true;
stepTimer.stop();
setMicrostep(16);
stepTimer.start(trackingRate, true);
}

void Axis::stopTracking() {
trackingActive = false;
stepTimer.stop();
void Axis::stopTracking()
{
trackingActive = false;
stepTimer.stop();
}

void Axis::startSlew(uint64_t rate, bool directionArg) {
stepTimer.stop();
axisAbsoluteDirection = directionArg;
setDirection(axisAbsoluteDirection);
slewActive = true;
setMicrostep(8);
slewTimeOut.start(12000, true);
stepTimer.start(rate, true);
void Axis::startSlew(uint64_t rate, bool directionArg)
{
stepTimer.stop();
axisAbsoluteDirection = directionArg;
setDirection(axisAbsoluteDirection);
slewActive = true;
setMicrostep(8);
slewTimeOut.start(12000, true);
stepTimer.start(rate, true);
}

void Axis::stopSlew() {
slewActive = false;
stepTimer.stop();
slewTimeOut.stop();
if (trackingActive) {
startTracking(trackingRate, trackingDirection);
}
void Axis::stopSlew()
{
slewActive = false;
stepTimer.stop();
slewTimeOut.stop();
if (trackingActive)
{
startTracking(trackingRate, trackingDirection);
}
}

void Axis::setAxisTargetCount(int64_t count) {
targetCount = count;
void Axis::setAxisTargetCount(int64_t count)
{
targetCount = count;
}

void Axis::resetAxisCount() {
axisCountValue = 0;
void Axis::resetAxisCount()
{
axisCountValue = 0;
}

void Axis::setDirection(bool directionArg) {
digitalWrite(dirPin, directionArg ^ invertDirectionPin);
void Axis::setDirection(bool directionArg)
{
digitalWrite(dirPin, directionArg ^ invertDirectionPin);
}

void Axis::setMicrostep(uint8_t microstep) {
switch (microstep) {
case 8:
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
break;
case 16:
digitalWrite(MS1, HIGH);
digitalWrite(MS2, HIGH);
break;
case 32:
digitalWrite(MS1, HIGH);
digitalWrite(MS2, LOW);
break;
case 64:
digitalWrite(MS1, LOW);
digitalWrite(MS2, HIGH);
break;
}
void Axis::setMicrostep(uint8_t microstep)
{
switch (microstep)
{
case 8:
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
break;
case 16:
digitalWrite(MS1, HIGH);
digitalWrite(MS2, HIGH);
break;
case 32:
digitalWrite(MS1, HIGH);
digitalWrite(MS2, LOW);
break;
case 64:
digitalWrite(MS1, LOW);
digitalWrite(MS2, HIGH);
break;
}
}
87 changes: 45 additions & 42 deletions esp32_wireless_control/firmware/axis.h
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
#ifndef AXIS_H
#define AXIS_H

#include "hardwaretimer.h"
#include "config.h"
#include "hardwaretimer.h"

extern HardwareTimer slewTimeOut;

#ifdef STEPPER_0_9
enum trackingRateS { TRACKING_SIDEREAL = 2659383, //SIDEREAL (23h,56 min)
TRACKING_SOLAR = 2666666, //SOLAR (24h)
TRACKING_LUNAR = 2723867 }; //LUNAR (24h, 31 min)
#else //stepper 1.8 deg
enum trackingRateS { TRACKING_SIDEREAL = 5318765, //SIDEREAL (23h,56 min)
TRACKING_SOLAR = 5333333, //SOLAR (24h)
TRACKING_LUNAR = 5447735 }; //LUNAR (24h, 31 min)
enum trackingRateS
{
TRACKING_SIDEREAL = 2659383, // SIDEREAL (23h,56 min)
TRACKING_SOLAR = 2666666, // SOLAR (24h)
TRACKING_LUNAR = 2723867, // LUNAR (24h, 31 min)
};
#else // stepper 1.8 deg
enum trackingRateS
{
TRACKING_SIDEREAL = 5318765, // SIDEREAL (23h,56 min)
TRACKING_SOLAR = 5333333, // SOLAR (24h)
TRACKING_LUNAR = 5447735, // LUNAR (24h, 31 min)
};
#endif


class Axis {


public:

Axis(uint8_t axisNumber, uint8_t dirPinforAxis, bool invertDirPin);
void startTracking(trackingRateS rate, bool directionArg);
void stopTracking();
void startSlew(uint64_t rate, bool directionArg);
void stopSlew();
volatile int64_t axisCountValue;
volatile int64_t targetCount;
volatile bool goToTarget;
bool slewActive;
bool trackingActive;
volatile bool axisAbsoluteDirection;
bool trackingDirection;
volatile bool counterActive;
void setAxisTargetCount(int64_t count);
void resetAxisCount();

trackingRateS trackingRate;

private:
void setDirection(bool directionArg);
static void setMicrostep(uint8_t microstep);
HardwareTimer stepTimer;
uint8_t stepPin;
uint8_t dirPin;
uint8_t axisNumber;
bool invertDirectionPin;
static const uint8_t MS1Pin = MS1;
static const uint8_t MS2Pin = MS2;
class Axis
{
public:
Axis(uint8_t axisNumber, uint8_t dirPinforAxis, bool invertDirPin);
void startTracking(trackingRateS rate, bool directionArg);
void stopTracking();
void startSlew(uint64_t rate, bool directionArg);
void stopSlew();
volatile int64_t axisCountValue;
volatile int64_t targetCount;
volatile bool goToTarget;
bool slewActive;
bool trackingActive;
volatile bool axisAbsoluteDirection;
bool trackingDirection;
volatile bool counterActive;
void setAxisTargetCount(int64_t count);
void resetAxisCount();

trackingRateS trackingRate;

private:
void setDirection(bool directionArg);
static void setMicrostep(uint8_t microstep);
HardwareTimer stepTimer;
uint8_t stepPin;
uint8_t dirPin;
uint8_t axisNumber;
bool invertDirectionPin;
static const uint8_t MS1Pin = MS1;
static const uint8_t MS2Pin = MS2;
};

extern Axis ra_axis;
Expand Down
14 changes: 7 additions & 7 deletions esp32_wireless_control/firmware/config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef CONFIG
#define CONFIG

/* clang-format off */
/*To build in Arduino IDE, install esp32 boards V3.0x and the Arduinojson library by Benoit Blanchon*/
/*****USER DEFINED*****/
//AP mode by default: ESP32 will create a wifi network which you can connect to
Expand All @@ -14,25 +15,24 @@
#define STEPPER_0_9 //uncomment this line if you have a 0.9 degree NEMA17
//#define STEPPER_1_8 //uncomment this line if you have a 1.8 degree NEMA17, and comment the above line
/**********************/


/* clang-format on */

/*****DO NOT MODIFY BELOW*****/
//LEDs for intervalometer status and general purpose status led
// LEDs for intervalometer status and general purpose status led
#define INTERV_PIN 25
#define STATUS_LED 26
#define PRESETS_EEPROM_START_LOCATION 1

//Stepper driver pins -- intended for TMC2209 for now
//AXIS 1 - RA
// Stepper driver pins -- intended for TMC2209 for now
// AXIS 1 - RA
#define AXIS1_STEP 5
#define AXIS1_DIR 15
#define SPREAD_1 4
//AXIS 2 - DEC
// AXIS 2 - DEC
#define AXIS2_STEP 19
#define AXIS2_DIR 18
#define SPREAD_2 21
//common pins
// common pins
#define MS1 23
#define MS2 22
#define EN12_n 17
Expand Down
Loading

0 comments on commit db39777

Please sign in to comment.