Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. extern "C" {
  3. #include "user_interface.h"
  4. }
  5.  
  6. // motors vars
  7. #define m1o1 5
  8. #define m1o2 4
  9. #define m1o3 0
  10. #define m1o4 2
  11. #define m2o1 16
  12. #define m2o2 14
  13. #define m2o3 12
  14. #define m2o4 13
  15. // wifi vars
  16. #define ssid "robots_flock"
  17. #define password "qwerfdsa"
  18. #define serverIP "192.168.10.200"
  19.  
  20. os_timer_t myTimer;
  21. volatile int motorState[] = {0,0};
  22. volatile bool rotating[] = {true,true};
  23. volatile bool rot_direction[] = {false,true};
  24. volatile int motorPinsState[] = {0,0,0,0};
  25.  
  26.  
  27.  
  28. int getStepFull(int motor = 0) {
  29. // full step mode
  30. switch(motorState[motor]) {
  31. case 0:
  32. motorPinsState[0] = HIGH;
  33. motorPinsState[1] = HIGH;
  34. motorPinsState[2] = LOW;
  35. motorPinsState[3] = LOW;
  36. break;
  37. case 1:
  38. motorPinsState[0] = LOW;
  39. motorPinsState[1] = HIGH;
  40. motorPinsState[2] = HIGH;
  41. motorPinsState[3] = LOW;
  42. break;
  43. case 2:
  44. motorPinsState[0] = LOW;
  45. motorPinsState[1] = LOW;
  46. motorPinsState[2] = HIGH;
  47. motorPinsState[3] = HIGH;
  48. break;
  49. case 3:
  50. motorPinsState[0] = HIGH;
  51. motorPinsState[1] = LOW;
  52. motorPinsState[2] = LOW;
  53. motorPinsState[3] = HIGH;
  54. break;
  55. }
  56. motorState[motor] += rot_direction[motor] ? 1 : -1;
  57. if (motorState[motor] == 4) motorState[motor] = 0;
  58. if (motorState[motor] == -1) motorState[motor] = 3;
  59. }
  60.  
  61. void getStepHalf(int motor = 0) {
  62. // half step mode (2x slower)
  63. switch(motorState[motor]) {
  64. case 0:
  65. motorPinsState[0] = HIGH;
  66. motorPinsState[1] = LOW;
  67. motorPinsState[2] = LOW;
  68. motorPinsState[3] = LOW;
  69. break;
  70. case 1:
  71. motorPinsState[0] = HIGH;
  72. motorPinsState[1] = HIGH;
  73. motorPinsState[2] = LOW;
  74. motorPinsState[3] = LOW;
  75. break;
  76. case 2:
  77. motorPinsState[0] = LOW;
  78. motorPinsState[1] = HIGH;
  79. motorPinsState[2] = LOW;
  80. motorPinsState[3] = LOW;
  81. break;
  82. case 3:
  83. motorPinsState[0] = LOW;
  84. motorPinsState[1] = HIGH;
  85. motorPinsState[2] = HIGH;
  86. motorPinsState[3] = LOW;
  87. break;
  88. case 4:
  89. motorPinsState[0] = LOW;
  90. motorPinsState[1] = LOW;
  91. motorPinsState[2] = HIGH;
  92. motorPinsState[3] = LOW;
  93. break;
  94. case 5:
  95. motorPinsState[0] = LOW;
  96. motorPinsState[1] = LOW;
  97. motorPinsState[2] = HIGH;
  98. motorPinsState[3] = HIGH;
  99. break;
  100. case 6:
  101. motorPinsState[0] = LOW;
  102. motorPinsState[1] = LOW;
  103. motorPinsState[2] = LOW;
  104. motorPinsState[3] = HIGH;
  105. break;
  106. case 7:
  107. motorPinsState[0] = HIGH;
  108. motorPinsState[1] = LOW;
  109. motorPinsState[2] = LOW;
  110. motorPinsState[3] = HIGH;
  111. break;
  112. }
  113.  
  114. motorState[motor] += rot_direction[motor] ? 1 : -1;
  115. if (motorState[motor] == 8) motorState[motor] = 0;
  116. if (motorState[motor] == -1) motorState[motor] = 7;
  117. }
  118.  
  119. void timer(void *pArg) {
  120. // get steps for 1 motor
  121. if(rotating[0]) {
  122. getStepFull(0);
  123. digitalWrite(m1o1, motorPinsState[0]);
  124. digitalWrite(m1o2, motorPinsState[1]);
  125. digitalWrite(m1o3, motorPinsState[2]);
  126. digitalWrite(m1o4, motorPinsState[3]);
  127. }
  128. // get steps for 2 motor
  129. if(rotating[1]) {
  130. getStepFull(1);
  131. digitalWrite(m2o1, motorPinsState[0]);
  132. digitalWrite(m2o2, motorPinsState[1]);
  133. digitalWrite(m2o3, motorPinsState[2]);
  134. digitalWrite(m2o4, motorPinsState[3]);
  135. }
  136. }
  137.  
  138. void connectWiFi() {
  139. WiFi.begin(ssid, password);
  140.  
  141. Serial.printf("Connecting to %s", ssid);
  142. while (WiFi.status() != WL_CONNECTED)
  143. {
  144. delay(500);
  145. Serial.print(".");
  146. }
  147.  
  148. Serial.print("\nConnected, IP address: ");
  149. Serial.println(WiFi.localIP());
  150.  
  151. }
  152.  
  153.  
  154. void setup() {
  155. pinMode(m1o1, OUTPUT); //to m1o1
  156. pinMode(m1o2, OUTPUT); //to m1o2
  157. pinMode(m1o3, OUTPUT); //to m1o3
  158. pinMode(m1o4, OUTPUT); //to m1o4
  159.  
  160. pinMode(m2o1, OUTPUT);
  161. pinMode(m2o2, OUTPUT);
  162. pinMode(m2o3, OUTPUT);
  163. pinMode(m2o4, OUTPUT);
  164.  
  165. // https://www.switchdoc.com/2015/10/iot-esp8266-timer-tutorial-arduino-ide/
  166. os_timer_setfn(&myTimer, timer, NULL);
  167. os_timer_arm(&myTimer, 2, true); // 2ms, repeated
  168. // only ONE timer :(
  169. ///
  170. // found out that the use of the os_timer set to a 2ms
  171. // interrupt will cause the WiFi to fail. We are investigating the cause,
  172. // but right now it definitely kills the WiFi connection and will not reconnect.
  173.  
  174. Serial.begin(115200);
  175. Serial.println();
  176. connectWiFi();
  177. }
  178.  
  179. void loop() {
  180. WiFiClient client;
  181.  
  182. Serial.printf("\nConnecting to %s:1337 ... ", serverIP);
  183. if (client.connect(serverIP, 1337))
  184. {
  185. Serial.println("connected");
  186.  
  187. while (client.connected() || client.available())
  188. {
  189. if (client.available())
  190. {
  191. String line = client.readStringUntil('\n');
  192. rotating[0] = line[1] != 48; // != '0'
  193. rot_direction[0] = line[1] == 102; // == 'f'
  194. rotating[1] = line[3] != 48; // != '0'
  195. rot_direction[1] = line[3] == 102; // == 'f'
  196. Serial.printf("rot1:%d, rotdir1:%d\n", rotating[0], rot_direction[0]);
  197. }
  198. }
  199. client.stop();
  200. Serial.println("\nDisconnected");
  201. }
  202. else
  203. {
  204. Serial.println("connection failed!");
  205. client.stop();
  206. }
  207. delay(2000);
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement