Remote controller for 3D printer. Arduino site.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FSRemotePowerSwitch.ino 6.5KB

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