Remote controller for 3D printer. Arduino site.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FSRemotePowerSwitch.ino 6.3KB

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