Controller.ino 8.8 KB

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