|
|
|
@@ -14,6 +14,7 @@ |
|
|
|
#include <AccelStepper.h> |
|
|
|
#include <HardwareSerial.h> |
|
|
|
#include <math.h> |
|
|
|
#include <EEPROM.h> |
|
|
|
|
|
|
|
const char *Version = "V1.3.0"; |
|
|
|
|
|
|
|
@@ -39,8 +40,14 @@ const String CMD_VAL_OFF = "Off"; |
|
|
|
const int PWR_RELAY_PIN = 5; |
|
|
|
const int PWR_CAM_PIN = 6; |
|
|
|
|
|
|
|
const int eepromAddrStatusRelay = 100; |
|
|
|
const int eepromAddrStatusCam = 200; |
|
|
|
const int eepromAddrStatusCamVel = 300; |
|
|
|
const int eepromAddrActAzimut = 400; |
|
|
|
|
|
|
|
JsonDocument msg; |
|
|
|
String stepperVelocity; |
|
|
|
int actAzimut; |
|
|
|
|
|
|
|
// Define motor interface type |
|
|
|
#define motorInterfaceType AccelStepper::MotorInterfaceType::HALF4WIRE |
|
|
|
@@ -54,11 +61,32 @@ void setup() { |
|
|
|
pinMode(PWR_RELAY_PIN, OUTPUT); |
|
|
|
pinMode(PWR_CAM_PIN, OUTPUT); |
|
|
|
|
|
|
|
setSlowStepperVelocity(); // Slow: Acc: 500 / Fast: MaxSpeed: 1000 |
|
|
|
// Slow: Acc: 200 / Fast: MaxSpeed: 500 |
|
|
|
int velocity = EEPROM.read(eepromAddrStatusCamVel); |
|
|
|
if (velocity == 0) { |
|
|
|
setSlowStepperVelocity(); // Slow: Acc: 200 / Slow: MaxSpeed: 500 |
|
|
|
} else { |
|
|
|
setFastStepperVelocity(); // Fast: Acc: 500 / Fast: MaxSpeed: 1000 |
|
|
|
} |
|
|
|
|
|
|
|
int powerRel = EEPROM.read(eepromAddrStatusRelay); |
|
|
|
if (powerRel == 0) { |
|
|
|
digitalWrite(PWR_RELAY_PIN, LOW); |
|
|
|
} else { |
|
|
|
digitalWrite(PWR_RELAY_PIN, HIGH); |
|
|
|
} |
|
|
|
|
|
|
|
int camRel = EEPROM.read(eepromAddrStatusCam); |
|
|
|
if (powerRel == 0) { |
|
|
|
digitalWrite(PWR_CAM_PIN, LOW); |
|
|
|
} else { |
|
|
|
digitalWrite(PWR_CAM_PIN, HIGH); |
|
|
|
} |
|
|
|
|
|
|
|
digitalWrite(PWR_RELAY_PIN, HIGH); |
|
|
|
digitalWrite(PWR_CAM_PIN, HIGH); |
|
|
|
actAzimut = EEPROM.read(eepromAddrActAzimut); |
|
|
|
if (actAzimut > 180) { |
|
|
|
actAzimut = 0; |
|
|
|
} |
|
|
|
stepper.setCurrentPosition(round((4076.0 / 360.0) * ((double) actAzimut) * -1)); |
|
|
|
} |
|
|
|
|
|
|
|
/* The loop function runs over and and checks the serial port for receiving a Json of following format: |
|
|
|
@@ -109,9 +137,11 @@ void loop() { |
|
|
|
if (digitalRead(PWR_RELAY_PIN) == LOW) { |
|
|
|
moveStepper(); |
|
|
|
sendRelayPowerStatus(CMD_VAL_ON); |
|
|
|
EEPROM.write(eepromAddrStatusRelay, 0); |
|
|
|
} else { |
|
|
|
moveStepper(); |
|
|
|
sendRelayPowerStatus(CMD_VAL_OFF); |
|
|
|
EEPROM.write(eepromAddrStatusRelay, 1); |
|
|
|
} |
|
|
|
|
|
|
|
} else if (sdr == CMD_SENDER_PC && cmd == CMD_STATUS_RELAY) { |
|
|
|
@@ -137,9 +167,11 @@ void loop() { |
|
|
|
if (digitalRead(PWR_CAM_PIN) == LOW) { |
|
|
|
moveStepper(); |
|
|
|
sendCamPowerStatus(CMD_VAL_ON); |
|
|
|
EEPROM.write(eepromAddrStatusCam, 0); |
|
|
|
} else { |
|
|
|
moveStepper(); |
|
|
|
sendCamPowerStatus(CMD_VAL_OFF); |
|
|
|
EEPROM.write(eepromAddrStatusCam, 1); |
|
|
|
} |
|
|
|
|
|
|
|
} else if (sdr == CMD_SENDER_PC && cmd == CMD_STATUS_CAM) { |
|
|
|
@@ -152,10 +184,6 @@ void loop() { |
|
|
|
sendCamPowerStatus(CMD_VAL_OFF); |
|
|
|
} |
|
|
|
|
|
|
|
} else if (sdr == CMD_SENDER_PC && cmd == CMD_ZERO_CAM) { |
|
|
|
stepper.setCurrentPosition(0); |
|
|
|
sendZeroCamStatus(String(stepper.currentPosition())); |
|
|
|
|
|
|
|
} else if (sdr == CMD_SENDER_PC && cmd == CMD_VELOCITY_CAM) { |
|
|
|
if (val.equals(CMD_VELOCITY_FAST)) { |
|
|
|
setFastStepperVelocity(); |
|
|
|
@@ -167,6 +195,11 @@ void loop() { |
|
|
|
} else if (sdr == CMD_SENDER_PC && cmd == CMD_STATUS_VELOCITY_CAM) { |
|
|
|
sendCamVelocityStatus(); |
|
|
|
|
|
|
|
} else if (sdr == CMD_SENDER_PC && cmd == CMD_ZERO_CAM) { |
|
|
|
stepper.setCurrentPosition(0); |
|
|
|
sendZeroCamStatus(String(stepper.currentPosition())); |
|
|
|
EEPROM.write(eepromAddrActAzimut, 0); |
|
|
|
|
|
|
|
} else if (sdr == CMD_SENDER_PC && cmd == CMD_ACT_AZIMUT_CAM) { |
|
|
|
int actAzi = round(stepper.currentPosition() / (4076.0 / 360.0) * -1); |
|
|
|
sendCamActAzimut(String(actAzi)); |
|
|
|
@@ -176,6 +209,7 @@ void loop() { |
|
|
|
long target = round((4076.0 / 360.0) * val.toDouble() * -1); |
|
|
|
stepper.moveTo(target); |
|
|
|
stepper.run(); |
|
|
|
EEPROM.write(eepromAddrActAzimut, val.toInt()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@@ -210,6 +244,7 @@ void setFastStepperVelocity() { |
|
|
|
stepper.setMaxSpeed(1000); // Slow: 500 / Fast: 1000 |
|
|
|
stepper.setAcceleration(500); // Slow: 200 / Fast: 500 |
|
|
|
stepper.setSpeed(1000); |
|
|
|
EEPROM.write(eepromAddrStatusCamVel, 1); |
|
|
|
} |
|
|
|
|
|
|
|
void setSlowStepperVelocity() { |
|
|
|
@@ -218,9 +253,9 @@ void setSlowStepperVelocity() { |
|
|
|
stepper.setMaxSpeed(500); // Slow: 500 / Fast: 1000 |
|
|
|
stepper.setAcceleration(200); // Slow: 200 / Fast: 500 |
|
|
|
stepper.setSpeed(500); |
|
|
|
EEPROM.write(eepromAddrStatusCamVel, 0); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void sendVersion() { |
|
|
|
msg["Sender"] = CMD_SENDER_AN; |
|
|
|
msg["Cmd"] = CMD_VER; |
|
|
|
@@ -263,7 +298,7 @@ void sendCamActAzimut(String val) { |
|
|
|
|
|
|
|
void sendCamVelocityStatus() { |
|
|
|
msg["Sender"] = CMD_SENDER_AN; |
|
|
|
msg["Cmd"] = CMD_VELOCITY_CAM; |
|
|
|
msg["Cmd"] = CMD_STATUS_VELOCITY_CAM; |
|
|
|
msg["Val"] = stepperVelocity; |
|
|
|
|
|
|
|
serializeJson(msg, Serial); |