Controller.ino 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "I2Cdev.h"
  2. #include "MPU6050_6Axis_MotionApps20.h"
  3. #include <Wire.h>
  4. #include <WiFiUdp.h>
  5. #include <ESP8266WiFi.h>
  6. #include <EEPROM.h>
  7. #include <Adafruit_ADS1015.h>
  8. String WifiSSIDPrefix = "CrystalPoint";
  9. uint16_t BasestationPort = 2730;
  10. IPAddress BasestationIp = IPAddress(192, 168, 4, 1);
  11. Adafruit_ADS1115 ads;
  12. struct MPU6050Data{
  13. int yaw;
  14. int pitch;
  15. int roll;
  16. };
  17. struct JoystickData{
  18. int x;
  19. int y;
  20. char button;
  21. };
  22. struct SwitchData{
  23. char backSwitch;
  24. char magnetSwitch;
  25. };
  26. //mpu6050
  27. MPU6050 mpu;
  28. bool dmpReady = false; // set true if DMP init was successful
  29. uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
  30. uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
  31. uint16_t fifoCount; // count of all bytes currently in FIFO
  32. uint8_t fifoBuffer[64]; // FIFO storage buffer
  33. // orientation/motion vars
  34. Quaternion q; // [w, x, y, z] quaternion container
  35. VectorFloat gravity; // [x, y, z] gravity vector
  36. float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
  37. volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
  38. void dmpDataReady() {
  39. mpuInterrupt = true;
  40. }
  41. //Joystick offsets
  42. int joystickoffset_x, joystickoffset_y;
  43. //Data buffer for sensors
  44. struct MPU6050Data mpu6050data = {0};
  45. struct JoystickData joystickdata = {0};
  46. struct SwitchData switchdata = {0};
  47. //UDP server (receiving) setup
  48. unsigned int udpPort = 2730;
  49. byte packetBuffer[512]; //udp package buffer
  50. char sendBuffer[45];
  51. WiFiUDP Udp;
  52. void setup(void){
  53. Serial.begin(115200);
  54. Serial.println("Starting controller..");
  55. WiFi.mode(WIFI_STA);
  56. WiFi.disconnect();
  57. EEPROM.begin(512);
  58. delay(10);
  59. Udp.begin(udpPort);
  60. Wire.begin();
  61. //Magnetic sensor
  62. pinMode(13,INPUT);
  63. //clearEeprom();
  64. //connectLastBasestation();
  65. searchBasestation();
  66. delay(1000);
  67. ads.begin();
  68. joystickoffset_x = ads.readADC_SingleEnded(0);
  69. joystickoffset_y = ads.readADC_SingleEnded(1);
  70. mpu6050setup();
  71. }
  72. void mpu6050setup(){
  73. mpu.initialize();
  74. Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  75. int devStatus = mpu.dmpInitialize();
  76. mpu.setXGyroOffset(62);
  77. mpu.setYGyroOffset(25);
  78. mpu.setZGyroOffset(-11);
  79. mpu.setXAccelOffset(-2030);
  80. mpu.setYAccelOffset(11);
  81. mpu.setZAccelOffset(2119); // 1688 factory default for my test chip
  82. if (devStatus == 0) {
  83. mpu.setDMPEnabled(true);
  84. attachInterrupt(15, dmpDataReady, RISING); //intrupt pin on pin 15
  85. mpuIntStatus = mpu.getIntStatus();
  86. dmpReady = true;
  87. packetSize = mpu.dmpGetFIFOPacketSize();
  88. } else {
  89. Serial.print(F("DMP Initialization failed (code "));
  90. Serial.print(devStatus);
  91. Serial.println(F(")"));
  92. }
  93. }
  94. void loop(void){
  95. //wait for interupt from mpu6050, in the mean time pol udp server for new data
  96. while (!mpuInterrupt && fifoCount < packetSize) {
  97. //receiving rumble or shock data from basestation
  98. int noBytes = Udp.parsePacket();
  99. if ( noBytes ) {
  100. Serial.print(millis() / 1000);
  101. Serial.print(":Packet of ");
  102. Serial.print(noBytes);
  103. Serial.print(" received from ");
  104. Serial.print(Udp.remoteIP());
  105. Serial.print(":");
  106. Serial.println(Udp.remotePort());
  107. // We've received a packet, read the data from it
  108. Udp.read(packetBuffer,noBytes); // read the packet into the buffer
  109. // display the packet contents in HEX
  110. for (int i=1;i<=noBytes;i++){
  111. Serial.print(packetBuffer[i-1],HEX);
  112. if (i % 32 == 0){
  113. Serial.println();
  114. }
  115. else Serial.print(' ');
  116. }
  117. Serial.println();
  118. }
  119. delay(2);
  120. }
  121. //We are out of the while loop, so mpu interupt fired;
  122. readFifoMpu6050(&mpu6050data);
  123. readSwitches(&switchdata);
  124. readJoystick(&joystickdata);
  125. //Send new data
  126. joystickdata.button = 0;
  127. sendUdpMessage(&joystickdata, &mpu6050data, &switchdata);
  128. }
  129. void readJoystick(struct JoystickData *joystickdata){
  130. joystickdata->x = ads.readADC_SingleEnded(0) - joystickoffset_x;
  131. joystickdata->y = ads.readADC_SingleEnded(1) - joystickoffset_y;
  132. }
  133. void readFifoMpu6050(struct MPU6050Data *mpu6050data){
  134. // reset interrupt flag and get INT_STATUS byte
  135. mpuInterrupt = false;
  136. mpuIntStatus = mpu.getIntStatus();
  137. // get current FIFO count
  138. fifoCount = mpu.getFIFOCount();
  139. // check for overflow (this should never happen unless our code is too inefficient)
  140. if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  141. // reset so we can continue cleanly
  142. mpu.resetFIFO();
  143. Serial.println(F("FIFO overflow!"));
  144. } else if (mpuIntStatus & 0x02){
  145. // wait for correct available data length, should be a VERY short wait
  146. while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  147. // read a packet from FIFO
  148. mpu.getFIFOBytes(fifoBuffer, packetSize);
  149. // track FIFO count here in case there is > 1 packet available
  150. // (this lets us immediately read more without waiting for an interrupt)
  151. fifoCount -= packetSize;
  152. // get Euler angles in degrees
  153. mpu.dmpGetQuaternion(&q, fifoBuffer);
  154. mpu.dmpGetGravity(&gravity, &q);
  155. mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  156. mpu6050data->yaw = ypr[0] * 18000/M_PI;
  157. mpu6050data->pitch = ypr[1] * 18000/M_PI;
  158. mpu6050data->roll = ypr[2] * 18000/M_PI;
  159. }
  160. }
  161. void readSwitches(struct SwitchData *switchdata){
  162. switchdata->magnetSwitch = !digitalRead(13);
  163. }
  164. void searchBasestation(void){
  165. Serial.print("Scanning for basestation ");
  166. for(int j = 0; j < 10; j++){
  167. int n = WiFi.scanNetworks();
  168. for (int i = 0; i < n; ++i){
  169. if(WiFi.SSID(i).startsWith(WifiSSIDPrefix)){ //We've found a basestation
  170. char WifiPassword[10];
  171. char WifiSSID[17];
  172. WiFi.SSID(i).toCharArray(WifiSSID, WiFi.SSID(i).length() + 1);
  173. calculateWiFiPassword(WiFi.SSID(i), WifiPassword);
  174. Serial.printf("Basestation found! SSID: %s", WifiSSID);
  175. Serial.printf("\nBasestation password: %s", WifiPassword);
  176. //Connect to the basestation
  177. WiFi.begin(WifiSSID, WifiPassword);
  178. while (WiFi.status() != WL_CONNECTED) {
  179. delay(500);
  180. Serial.print(".");
  181. }
  182. ///Writing data to eeprom
  183. writeEeprom(WifiSSID, WifiPassword);
  184. //Break out of the loop and stop scanning
  185. n = 254;
  186. j = 254;
  187. }
  188. }
  189. Serial.print(".");
  190. delay(1000);
  191. }
  192. }
  193. void connectLastBasestation(void){
  194. Serial.println("Reading information from EEPROM");
  195. char eepromWifiSSID[17];
  196. char eepromWifiPassword[10];
  197. //Read SSID
  198. for (int i = 0; i < 17; i++){
  199. eepromWifiSSID[i] = char(EEPROM.read(i));
  200. }
  201. eepromWifiSSID[17] = '\0';
  202. //Read Password
  203. for (int i = 0; i < 10; i++){
  204. eepromWifiPassword[i]= char(EEPROM.read(17+i));
  205. }
  206. eepromWifiPassword[10] = '\0';
  207. //Verify valid memory
  208. if(String(eepromWifiSSID).startsWith(WifiSSIDPrefix)){
  209. Serial.printf("Valid basestation information in eeprom found! \nEeprom SSID: %s \nEeprom Password: %s", eepromWifiSSID, eepromWifiPassword);
  210. //Connect to the basestation
  211. WiFi.begin(eepromWifiSSID, eepromWifiPassword);
  212. while (WiFi.status() != WL_CONNECTED) {
  213. delay(500);
  214. Serial.print(".");
  215. }
  216. }
  217. }
  218. void calculateWiFiPassword(String WifiSSID, char *WifiPassword)
  219. {
  220. String WifiStringPassword;
  221. for(char i=0; i < 17; i++){
  222. String s = String(WifiSSID[i], HEX);
  223. WifiStringPassword = s + WifiStringPassword;
  224. }
  225. for (int i=0; i < 10; i++)
  226. WifiPassword[i] = WifiStringPassword.charAt(i);
  227. WifiPassword[9] = '\0';
  228. }
  229. void sendUdpMessage(struct JoystickData *joystick, struct MPU6050Data *mpu6050data, struct SwitchData *switchdata){
  230. Udp.beginPacket(BasestationIp, BasestationPort);
  231. sprintf(sendBuffer, "%06d|%06d|%01d|", joystick->x, joystick->y, joystick->button);
  232. sprintf(sendBuffer, "%s%06d|%06d|%06d", sendBuffer, mpu6050data->yaw, mpu6050data->pitch, mpu6050data->roll);
  233. sprintf(sendBuffer, "%s%01d|%01d", sendBuffer, switchdata->backSwitch, switchdata->magnetSwitch);
  234. // Serial.printf("%s", sendBuffer);
  235. // Serial.println();
  236. Udp.write(sendBuffer);
  237. Udp.endPacket();
  238. }
  239. void writeEeprom(char *WifiSSID, char *WifiPassword){
  240. //Clean memory
  241. clearEeprom();
  242. //Write ssid
  243. for(int i = 0; i < 17; i++){
  244. EEPROM.write(i, WifiSSID[i]);
  245. }
  246. //Write password
  247. for(int i = 0; i < 10; i++){
  248. EEPROM.write(17+i, WifiPassword[i]);
  249. }
  250. EEPROM.commit();
  251. }
  252. void clearEeprom(){
  253. for (int i = 0; i < 30; i++) { EEPROM.write(i, 0); }
  254. }