Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiUdp.h>
  3.  
  4. const char* ssid = "RAILNET";
  5. const char* password = "it-seminar-2019";
  6.  
  7. #define MOTOR_PWM_PIN D1
  8. #define MOTOR_DIR_PIN D3
  9. #define HALL_SENSOR_A_PIN D5
  10. #define HALL_SENSOR_B_PIN D6
  11. #define WIFI_RESET_PIN D7
  12. #define ULTRA_SOUND_ECHO D8
  13. #define ULTRA_SOUND_TRIGG D4
  14. #define LED_PIN LED_BUILTIN
  15.  
  16. #define TRAIN_INFORMATION 2
  17. #define TRAIN_CONTROL 3
  18. #define TRAIN_BRAKE 4
  19.  
  20. #define I_AM_A_TRAIN 1
  21. #define I_AM_A_LEFT_SWITCH 2
  22. #define I_AM_A_RIGHT_SWITCH 3
  23. #define I_AM_A_SEMAPHORE 4
  24.  
  25.  
  26. WiFiUDP Udp;
  27. unsigned int localUdpPort = 4711; // local port to listen on
  28. char incomingPacket[255]; // buffer for incoming packets
  29. char outgoingPacket[255];
  30. char replyA[] = "At hall A";
  31. char replyB[] = "At hall B";
  32. char reply[] = "Hello there!";
  33.  
  34. int currentSpeed = 0;
  35. int currentDirection = 0;
  36. const int minSpeedEquivalent = 512;
  37. const int maxSpeedEquivalent = 800;
  38. const int maxSpeed = 200;
  39. int speed;
  40.  
  41. bool atStation = false;
  42. int magnetsPassed = 0;
  43.  
  44. bool onMagnet = false;
  45. unsigned long onMagnetTime = 0;
  46. int deltaCount = 0;
  47. IPAddress remoteIP = IPAddress(0,0,0,0);
  48. uint16_t remotePort = 0;
  49. unsigned long trainStopTime;
  50.  
  51. void setup() {
  52. pinMode(LED_PIN, OUTPUT);
  53. pinMode(MOTOR_PWM_PIN, OUTPUT);
  54. pinMode(MOTOR_DIR_PIN, OUTPUT);
  55. pinMode(HALL_SENSOR_A_PIN, INPUT_PULLUP);
  56. pinMode(HALL_SENSOR_B_PIN, INPUT_PULLUP);
  57. pinMode(ULTRA_SOUND_ECHO, INPUT);
  58. pinMode(ULTRA_SOUND_TRIGG, OUTPUT);
  59. attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_A_PIN), onHallA, FALLING);
  60. attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_B_PIN), onHallB, FALLING);
  61.  
  62. Serial.begin(115200);
  63. Serial.println();
  64. digitalWrite(LED_PIN, HIGH);
  65. Serial.printf("Connecting to %s ", ssid);
  66. WiFi.begin(ssid, password);
  67. while (WiFi.status() != WL_CONNECTED) {
  68. delay(500);
  69. Serial.print(".");
  70. }
  71. Serial.println(" connected");
  72.  
  73. Udp.begin(localUdpPort);
  74. Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
  75. digitalWrite(LED_PIN, LOW);
  76. }
  77.  
  78.  
  79. void loop()
  80. {
  81. US();
  82. int packetSize = Udp.parsePacket();
  83. if (packetSize)
  84. {
  85. remoteIP = Udp.remoteIP();
  86. remotePort = Udp.remotePort();
  87. // receive incoming UDP packets
  88. Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
  89. int len = Udp.read(incomingPacket, 255);
  90. if (len > 1)
  91. {
  92. if (incomingPacket[1] == TRAIN_CONTROL) {
  93. speed = readInt(incomingPacket, 2);
  94. Serial.printf("Speed: %d\n", speed);
  95. applySpeed(speed);
  96. }
  97. else if (incomingPacket[1] == TRAIN_BRAKE) {
  98. applySpeed(0);
  99. sendTrainInformation(0, 0, 0, 1);
  100. }
  101. else {
  102. incomingPacket[len] = 0;
  103. Serial.printf("UDP packet contents: %s\n", incomingPacket);
  104. }
  105.  
  106. }
  107.  
  108. // send back a reply, to the IP address and port we got the packet from
  109. }
  110. }
  111.  
  112. void US(){
  113. if(!atStation && magnetsPassed >= 2){
  114. long duration, distance;
  115. digitalWrite(ULTRA_SOUND_TRIGG, LOW);
  116. delayMicroseconds(2);
  117. digitalWrite(ULTRA_SOUND_TRIGG, HIGH);
  118. delayMicroseconds(10);
  119. digitalWrite(ULTRA_SOUND_TRIGG, LOW);
  120. duration = pulseIn(ULTRA_SOUND_ECHO, HIGH);
  121. distance = (duration/2) / 29.1;
  122. Serial.println(distance);
  123. if(distance < 20 ){
  124. trainStopTime = millis();
  125. Serial.print(" time: ");
  126. Serial.println(trainStopTime);
  127. applySpeed(0);
  128. atStation = true;
  129. magnetsPassed = 0;
  130. }
  131. }
  132. if(atStation && millis() - trainStopTime > 5000){
  133. applySpeed(speed);
  134. atStation = false;
  135. }
  136. }
  137.  
  138. void broadcastExistence() {
  139.  
  140. }
  141.  
  142. int readInt(char buffer[], int index) {
  143. int result = 0;
  144. for (int i = index + 3; i >= index; i--) {
  145. result <<= 8;
  146. result |= buffer[i];
  147. }
  148. return result;
  149. }
  150.  
  151. int writeInt(char buffer[], int index, int value) {
  152. for (int i = index; i < index + 4; i++) {
  153. buffer[i] = value & 255;
  154. value >>= 8;
  155. }
  156. return index + 4;
  157. }
  158.  
  159. void applySpeed(int speed) {
  160. int d = (maxSpeedEquivalent - minSpeedEquivalent)/maxSpeed;
  161. int speedEquivalent = minSpeedEquivalent + speed*d;
  162. if (speed == 0) speedEquivalent = 0;
  163. digitalWrite(MOTOR_DIR_PIN, currentDirection);
  164. analogWrite(MOTOR_PWM_PIN, speedEquivalent);
  165. }
  166.  
  167. void onHallA() {
  168. digitalWrite(LED_PIN, HIGH);
  169. Serial.printf("Magnet");
  170. if (onMagnet) return;
  171. onMagnet = true;
  172. onMagnetTime = millis();
  173. }
  174.  
  175. void onHallB() {
  176. digitalWrite(LED_PIN, LOW);
  177. if (remotePort == 0 || !onMagnet) return;
  178. onMagnet = false;
  179. onMagnetTime = millis() - onMagnetTime;
  180. sendTrainInformation((5000/onMagnetTime), 0, 0, 1);
  181. magnetsPassed++;
  182. }
  183.  
  184. void sendTrainInformation(int speed, int trackId, int distanceToLight, int light) {
  185. outgoingPacket[0] = 15;
  186. outgoingPacket[1] = TRAIN_INFORMATION;
  187. writeInt(outgoingPacket, 2, speed);
  188. writeInt(outgoingPacket, 6, trackId);
  189. writeInt(outgoingPacket, 10, distanceToLight);
  190. outgoingPacket[14] = light;
  191. Udp.beginPacket(remoteIP, remotePort);
  192. Udp.write(outgoingPacket, 15);
  193. Udp.endPacket();
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement