Remote controller for 3D printer. Arduino site.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

FSRemotePowerSwitch.ino 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * FSRemotePowerSwitch
  3. *
  4. * Allows to control an Arduino to power up a device.
  5. *
  6. * A send over cmd from a PC will set a digital pin to high, which
  7. * switches relay to close a 230V AC circuit.
  8. *
  9. *
  10. */
  11. #include "Arduino.h"
  12. #include <ArduinoJson.h>
  13. #include <AccelStepper.h>
  14. #include <HardwareSerial.h>
  15. #include <math.h>
  16. #include <EEPROM.h>
  17. const char *Version = "V1.3.0";
  18. const String CMD_VER = "Version";
  19. const String CMD_SENDER_PC = "FSRemotePowerSwitch_PC";
  20. const String CMD_SENDER_AN = "FSRemotePowerSwitch_AN";
  21. const String CMD_POWER_RELAY = "Power_Relay";
  22. const String CMD_STATUS_RELAY = "Status_Relay";
  23. const String CMD_POWER_CAM = "Power_Cam";
  24. const String CMD_STATUS_CAM = "Status_Cam";
  25. const String CMD_AZIMUT_CAM = "Azimut_Cam";
  26. const String CMD_ACT_AZIMUT_CAM = "Actual_Azimut_Cam";
  27. const String CMD_ZERO_CAM = "Zero_Cam";
  28. const String CMD_VELOCITY_CAM = "Velocity_Cam";
  29. const String CMD_STATUS_VELOCITY_CAM = "Status_Velocity_Cam";
  30. const String CMD_VELOCITY_SLOW = "Slow";
  31. const String CMD_VELOCITY_FAST = "Fast";
  32. const String CMD_VAL_ON = "On";
  33. const String CMD_VAL_OFF = "Off";
  34. const int PWR_RELAY_PIN = 5;
  35. const int PWR_CAM_PIN = 6;
  36. const int eepromAddrStatusRelay = 100;
  37. const int eepromAddrStatusCam = 200;
  38. const int eepromAddrStatusCamVel = 300;
  39. const int eepromAddrActAzimut = 400;
  40. JsonDocument msg;
  41. String stepperVelocity;
  42. int actAzimut;
  43. // Define motor interface type
  44. #define motorInterfaceType AccelStepper::MotorInterfaceType::HALF4WIRE
  45. // Creates an instance
  46. AccelStepper stepper(motorInterfaceType, 8, 10, 9, 11);
  47. // The setup function runs once when you press reset or power the board
  48. void setup() {
  49. Serial.begin(9600);
  50. pinMode(PWR_RELAY_PIN, OUTPUT);
  51. pinMode(PWR_CAM_PIN, OUTPUT);
  52. int velocity = EEPROM.read(eepromAddrStatusCamVel);
  53. if (velocity == 0) {
  54. setSlowStepperVelocity(); // Slow: Acc: 200 / Slow: MaxSpeed: 500
  55. } else {
  56. setFastStepperVelocity(); // Fast: Acc: 500 / Fast: MaxSpeed: 1000
  57. }
  58. int powerRel = EEPROM.read(eepromAddrStatusRelay);
  59. if (powerRel == 0) {
  60. digitalWrite(PWR_RELAY_PIN, LOW);
  61. } else {
  62. digitalWrite(PWR_RELAY_PIN, HIGH);
  63. }
  64. int camRel = EEPROM.read(eepromAddrStatusCam);
  65. if (powerRel == 0) {
  66. digitalWrite(PWR_CAM_PIN, LOW);
  67. } else {
  68. digitalWrite(PWR_CAM_PIN, HIGH);
  69. }
  70. actAzimut = EEPROM.read(eepromAddrActAzimut);
  71. if (actAzimut > 180) {
  72. actAzimut = 0;
  73. }
  74. stepper.setCurrentPosition(round((4076.0 / 360.0) * ((double) actAzimut) * -1));
  75. }
  76. /* The loop function runs over and and checks the serial port for receiving a Json of following format:
  77. *
  78. * JSON for FSRemotePowerSwitch
  79. *
  80. * {
  81. * "Sender": "FSRemotePowerSwitch_PC" or "FSRemotePowerSwitch_AN",
  82. * "Cmd": "Version" or "Status_Relay" or "Power_Relay" or "Status_Cam" or "Power_Cam" or "Azimut_Cam" or "Zero_Cam",
  83. * "Val": "V0.0.1" or "On"/"Off" or "0"
  84. * }
  85. *
  86. */
  87. void loop() {
  88. if (Serial.available() > 0) {
  89. moveStepper();
  90. String message = readSerialData();
  91. // Deserialize the JSON document
  92. DeserializationError error = deserializeJson(msg, message);
  93. // Test if parsing succeeds.
  94. if (error) {
  95. Serial.print(F("deserializeJson() failed: "));
  96. Serial.print(message);
  97. Serial.println(error.f_str());
  98. return;
  99. }
  100. message = "";
  101. String sdr = msg["Sender"];
  102. String cmd = msg["Cmd"];
  103. String val = msg["Val"];
  104. if (sdr == CMD_SENDER_PC && cmd == CMD_VER) {
  105. sendVersion();
  106. } else if (sdr == CMD_SENDER_PC && cmd == CMD_POWER_RELAY) {
  107. if (val == CMD_VAL_ON) {
  108. digitalWrite(PWR_RELAY_PIN, LOW);
  109. } else if (val == CMD_VAL_OFF) {
  110. digitalWrite(PWR_RELAY_PIN, HIGH);
  111. }
  112. if (digitalRead(PWR_RELAY_PIN) == LOW) {
  113. moveStepper();
  114. sendRelayPowerStatus(CMD_VAL_ON);
  115. EEPROM.write(eepromAddrStatusRelay, 0);
  116. } else {
  117. moveStepper();
  118. sendRelayPowerStatus(CMD_VAL_OFF);
  119. EEPROM.write(eepromAddrStatusRelay, 1);
  120. }
  121. } else if (sdr == CMD_SENDER_PC && cmd == CMD_STATUS_RELAY) {
  122. if (digitalRead(PWR_RELAY_PIN) == LOW) {
  123. moveStepper();
  124. sendRelayPowerStatus(CMD_VAL_ON);
  125. } else {
  126. moveStepper();
  127. sendRelayPowerStatus(CMD_VAL_OFF);
  128. }
  129. } else if (sdr == CMD_SENDER_PC && cmd == CMD_POWER_CAM) {
  130. if (val == CMD_VAL_ON) {
  131. digitalWrite(PWR_CAM_PIN, LOW);
  132. stepper.move(10);
  133. moveStepper();
  134. } else if (val == CMD_VAL_OFF) {
  135. moveStepper();
  136. digitalWrite(PWR_CAM_PIN, HIGH);
  137. }
  138. if (digitalRead(PWR_CAM_PIN) == LOW) {
  139. moveStepper();
  140. sendCamPowerStatus(CMD_VAL_ON);
  141. EEPROM.write(eepromAddrStatusCam, 0);
  142. } else {
  143. moveStepper();
  144. sendCamPowerStatus(CMD_VAL_OFF);
  145. EEPROM.write(eepromAddrStatusCam, 1);
  146. }
  147. } else if (sdr == CMD_SENDER_PC && cmd == CMD_STATUS_CAM) {
  148. if (digitalRead(PWR_CAM_PIN) == LOW) {
  149. moveStepper();
  150. sendCamPowerStatus(CMD_VAL_ON);
  151. } else {
  152. moveStepper();
  153. sendCamPowerStatus(CMD_VAL_OFF);
  154. }
  155. } else if (sdr == CMD_SENDER_PC && cmd == CMD_VELOCITY_CAM) {
  156. if (val.equals(CMD_VELOCITY_FAST)) {
  157. setFastStepperVelocity();
  158. } else if (val.equals(CMD_VELOCITY_SLOW)) {
  159. setSlowStepperVelocity();
  160. }
  161. sendCamVelocityStatus();
  162. } else if (sdr == CMD_SENDER_PC && cmd == CMD_STATUS_VELOCITY_CAM) {
  163. sendCamVelocityStatus();
  164. } else if (sdr == CMD_SENDER_PC && cmd == CMD_ZERO_CAM) {
  165. stepper.setCurrentPosition(0);
  166. sendZeroCamStatus(String(stepper.currentPosition()));
  167. EEPROM.write(eepromAddrActAzimut, 0);
  168. } else if (sdr == CMD_SENDER_PC && cmd == CMD_ACT_AZIMUT_CAM) {
  169. int actAzi = round(stepper.currentPosition() / (4076.0 / 360.0) * -1);
  170. sendCamActAzimut(String(actAzi));
  171. } else if (sdr == CMD_SENDER_PC && cmd == CMD_AZIMUT_CAM) {
  172. if (digitalRead(PWR_CAM_PIN) == LOW) {
  173. long target = round((4076.0 / 360.0) * val.toDouble() * -1);
  174. stepper.moveTo(target);
  175. stepper.run();
  176. EEPROM.write(eepromAddrActAzimut, val.toInt());
  177. }
  178. }
  179. }
  180. moveStepper();
  181. }
  182. void moveStepper() {
  183. if (stepper.distanceToGo() != 0) {
  184. // Move the motor one step
  185. stepper.run();
  186. }
  187. }
  188. String readSerialData() {
  189. String msg = "";
  190. String data = "";
  191. while (Serial.available() > 0) {
  192. data = Serial.readString();
  193. msg += data;
  194. //delay(50);
  195. }
  196. return msg;
  197. }
  198. void setFastStepperVelocity() {
  199. stepperVelocity = CMD_VELOCITY_FAST;
  200. stepper.setCurrentPosition(stepper.currentPosition());
  201. stepper.setMaxSpeed(1000); // Slow: 500 / Fast: 1000
  202. stepper.setAcceleration(500); // Slow: 200 / Fast: 500
  203. stepper.setSpeed(1000);
  204. EEPROM.write(eepromAddrStatusCamVel, 1);
  205. }
  206. void setSlowStepperVelocity() {
  207. stepperVelocity = CMD_VELOCITY_SLOW;
  208. stepper.setCurrentPosition(stepper.currentPosition());
  209. stepper.setMaxSpeed(500); // Slow: 500 / Fast: 1000
  210. stepper.setAcceleration(200); // Slow: 200 / Fast: 500
  211. stepper.setSpeed(500);
  212. EEPROM.write(eepromAddrStatusCamVel, 0);
  213. }
  214. void sendVersion() {
  215. msg["Sender"] = CMD_SENDER_AN;
  216. msg["Cmd"] = CMD_VER;
  217. msg["Val"] = Version;
  218. serializeJson(msg, Serial);
  219. }
  220. void sendRelayPowerStatus(String status) {
  221. msg["Sender"] = CMD_SENDER_AN;
  222. msg["Cmd"] = CMD_STATUS_RELAY;
  223. msg["Val"] = status;
  224. serializeJson(msg, Serial);
  225. }
  226. void sendCamPowerStatus(String status) {
  227. msg["Sender"] = CMD_SENDER_AN;
  228. msg["Cmd"] = CMD_STATUS_CAM;
  229. msg["Val"] = status;
  230. serializeJson(msg, Serial);
  231. }
  232. void sendZeroCamStatus(String val) {
  233. msg["Sender"] = CMD_SENDER_AN;
  234. msg["Cmd"] = CMD_ZERO_CAM;
  235. msg["Val"] = val;
  236. serializeJson(msg, Serial);
  237. }
  238. void sendCamActAzimut(String val) {
  239. msg["Sender"] = CMD_SENDER_AN;
  240. msg["Cmd"] = CMD_ACT_AZIMUT_CAM;
  241. msg["Val"] = val;
  242. serializeJson(msg, Serial);
  243. }
  244. void sendCamVelocityStatus() {
  245. msg["Sender"] = CMD_SENDER_AN;
  246. msg["Cmd"] = CMD_STATUS_VELOCITY_CAM;
  247. msg["Val"] = stepperVelocity;
  248. serializeJson(msg, Serial);
  249. }