Controller.ino 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "I2Cdev.h"
  2. #include "MPU6050.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. int16_t ax, ay, az;
  29. int16_t gx, gy, gz;
  30. double ax_scaled, ay_scaled, az_scaled;
  31. double gx_scaled, gy_scaled, gz_scaled;
  32. double roll, pitch, yaw;
  33. double gx_off, gy_off, gz_off;
  34. double gyro_scale = 131.0;
  35. double accel_scale = 16384.0;
  36. //Joystick offsets
  37. int joystickoffset_x, joystickoffset_y;
  38. //rumble
  39. boolean rumbleActivated;
  40. u_long rumbleDuration;
  41. //Data buffer for sensors
  42. struct MPU6050Data mpu6050data = {0};
  43. struct JoystickData joystickdata = {0};
  44. struct SwitchData switchdata = {0};
  45. //UDP server (receiving) setup
  46. unsigned int udpPort = 2730;
  47. byte packetBuffer[50]; //udp package buffer
  48. char sendBuffer[50];
  49. WiFiUDP Udp;
  50. void setup(void){
  51. Serial.begin(115200);
  52. Serial.println("Starting controller..");
  53. WiFi.mode(WIFI_STA);
  54. WiFi.disconnect();
  55. EEPROM.begin(512);
  56. delay(10);
  57. Udp.begin(udpPort);
  58. Wire.begin();
  59. //Magnetic sensor
  60. pinMode(12,INPUT);
  61. //Rumble motor
  62. pinMode(15, OUTPUT);
  63. //clearEeprom();
  64. connectLastBasestation();
  65. //searchBasestation();
  66. delay(1000);
  67. ads.begin();
  68. joystickoffset_x = ads.readADC_SingleEnded(0, 1);
  69. joystickoffset_y = ads.readADC_SingleEnded(1, 1);
  70. mpu6050setup();
  71. }
  72. void mpu6050setup(){
  73. mpu.initialize();
  74. Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  75. calibrate();
  76. }
  77. void calibrate(){
  78. getMotion6Scaled();
  79. get_x_rotation(ax_scaled, ay_scaled, az_scaled, &roll);
  80. get_y_rotation(ax_scaled, ay_scaled, az_scaled, &pitch);
  81. yaw = 0;
  82. gx_off = gx_scaled;
  83. gy_off = gy_scaled;
  84. gz_off = gz_scaled;
  85. }
  86. void getMotion6Scaled(){
  87. mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  88. ax_scaled = ax / accel_scale;
  89. ay_scaled = ay / accel_scale;
  90. az_scaled = az / accel_scale;
  91. gx_scaled = gx / gyro_scale;
  92. gy_scaled = gy / gyro_scale;
  93. gz_scaled = gz / gyro_scale;
  94. }
  95. void get_y_rotation(double x, double y, double z, double* y_rotation){
  96. *y_rotation = ((atan2(x, dist(y, z))) * 4068.0) / 71.0;
  97. }
  98. void get_x_rotation(double x, double y, double z, double* x_rotation){
  99. *x_rotation = ((atan2(y, dist(x, z))) * -4068.0) / 71.0;
  100. }
  101. double dist(double a, double b){
  102. return sqrt((a * a) + (b * b));
  103. }
  104. u_long lasttime;
  105. void loop(void){
  106. readMpu6050(&mpu6050data);
  107. if(millis() - lasttime > 30){
  108. lasttime = millis();
  109. //Poll udp server
  110. int noBytes = Udp.parsePacket();
  111. if ( noBytes ) {
  112. Udp.read(packetBuffer,noBytes);
  113. if(packetBuffer[0] == '1'){
  114. char tmpstr[7];
  115. strncpy(tmpstr, (char*)packetBuffer+4, 6);
  116. int rumbleDuration = atoi(tmpstr);
  117. strncpy(tmpstr, (char*)packetBuffer+11, 3);
  118. int rumblePower = atoi(tmpstr);
  119. rumble(rumbleDuration, rumblePower);
  120. Serial.printf("Received udp package! Rumble duration: %d ~ Rumble power : %d", rumbleDuration, rumblePower);
  121. }else if(packetBuffer[0] == '2'){ //Shock
  122. }
  123. Serial.println();
  124. }
  125. if(rumbleActivated == true && millis() > rumbleDuration){
  126. rumbleActivated = false;
  127. analogWrite(15, 0);
  128. }
  129. readJoystick(&joystickdata);
  130. readSwitches(&switchdata);
  131. //Send new data
  132. joystickdata.button = 0;
  133. sendUdpMessage(&joystickdata, &mpu6050data, &switchdata);
  134. }else{
  135. delay(6);
  136. }
  137. }
  138. void readMpu6050(struct MPU6050Data *mpu6050data){
  139. getMotion6Scaled();
  140. gx_scaled -= gx_off;
  141. gy_scaled -= gy_off;
  142. gz_scaled -= gz_off;
  143. double gx_delta = (gx_scaled * 0.01);
  144. double gy_delta = (gy_scaled * 0.01);
  145. double gz_delta = (gz_scaled * 0.01);
  146. double rotationx, rotationy;
  147. get_y_rotation(ax_scaled, ay_scaled, az_scaled, &rotationy);
  148. get_x_rotation(ax_scaled, ay_scaled, az_scaled, &rotationx);
  149. pitch = 0.98 * (pitch + gy_delta) + (0.02 * rotationy);
  150. roll = 0.98 * (roll + gx_delta) + (0.02 * rotationx);
  151. mpu6050data->pitch = pitch*100;
  152. mpu6050data->roll = roll*100;
  153. mpu6050data->yaw = 0;
  154. mpu6050data->ax = ax_scaled;
  155. mpu6050data->ay = ay_scaled;
  156. mpu6050data->az = az_scaled;
  157. }
  158. void readJoystick(struct JoystickData *joystickdata){
  159. joystickdata->x = ads.readADC_SingleEnded(1, 0) - joystickoffset_x;
  160. delay(8);
  161. joystickdata->y = ads.readADC_SingleEnded(0, 0) - joystickoffset_y;
  162. }
  163. void readSwitches(struct SwitchData *switchdata){
  164. switchdata->magnetSwitch = !digitalRead(12);
  165. }
  166. void rumble(int duration, int power){
  167. rumbleActivated = true;
  168. analogWrite(15, power);
  169. rumbleDuration = millis() + duration;
  170. }
  171. void searchBasestation(void){
  172. Serial.print("Scanning for basestation ");
  173. for(int j = 0; j < 10; j++){
  174. int n = WiFi.scanNetworks();
  175. for (int i = 0; i < n; ++i){
  176. if(WiFi.SSID(i).startsWith(WifiSSIDPrefix)){ //We've found a basestation
  177. char WifiPassword[10];
  178. char WifiSSID[17];
  179. WiFi.SSID(i).toCharArray(WifiSSID, WiFi.SSID(i).length() + 1);
  180. calculateWiFiPassword(WiFi.SSID(i), WifiPassword);
  181. Serial.printf("Basestation found! SSID: %s", WifiSSID);
  182. Serial.printf("\nBasestation password: %s", WifiPassword);
  183. //Connect to the basestation
  184. WiFi.begin(WifiSSID, WifiPassword);
  185. while (WiFi.status() != WL_CONNECTED) {
  186. delay(500);
  187. Serial.print(".");
  188. }
  189. ///Writing data to eeprom
  190. writeEeprom(WifiSSID, WifiPassword);
  191. //Break out of the loop and stop scanning
  192. n = 254;
  193. j = 254;
  194. }
  195. }
  196. Serial.print(".");
  197. delay(1000);
  198. }
  199. }
  200. void connectLastBasestation(void){
  201. Serial.println("Reading information from EEPROM");
  202. char eepromWifiSSID[17];
  203. char eepromWifiPassword[10];
  204. //Read SSID
  205. for (int i = 0; i < 17; i++){
  206. eepromWifiSSID[i] = char(EEPROM.read(i));
  207. }
  208. eepromWifiSSID[17] = '\0';
  209. //Read Password
  210. for (int i = 0; i < 10; i++){
  211. eepromWifiPassword[i]= char(EEPROM.read(17+i));
  212. }
  213. eepromWifiPassword[10] = '\0';
  214. //Verify valid memory
  215. if(String(eepromWifiSSID).startsWith(WifiSSIDPrefix)){
  216. Serial.printf("Valid basestation information in eeprom found! \nEeprom SSID: %s \nEeprom Password: %s", eepromWifiSSID, eepromWifiPassword);
  217. //Connect to the basestation
  218. WiFi.begin(eepromWifiSSID, eepromWifiPassword);
  219. while (WiFi.status() != WL_CONNECTED) {
  220. delay(500);
  221. Serial.print(".");
  222. }
  223. }
  224. }
  225. void calculateWiFiPassword(String WifiSSID, char *WifiPassword)
  226. {
  227. String WifiStringPassword;
  228. for(char i=0; i < 17; i++){
  229. String s = String(WifiSSID[i], HEX);
  230. WifiStringPassword = s + WifiStringPassword;
  231. }
  232. for (int i=0; i < 10; i++)
  233. WifiPassword[i] = WifiStringPassword.charAt(i);
  234. WifiPassword[9] = '\0';
  235. }
  236. void sendUdpMessage(struct JoystickData *joystick, struct MPU6050Data *mpu6050data, struct SwitchData *switchdata){
  237. Udp.beginPacket(BasestationIp, BasestationPort);
  238. sprintf(sendBuffer, "%06d|%06d|%01d|", joystick->x, joystick->y, joystick->button);
  239. sprintf(sendBuffer, "%s%06d|%06d|%06d|", sendBuffer, mpu6050data->yaw, mpu6050data->pitch, mpu6050data->roll);
  240. sprintf(sendBuffer, "%s%01d|%01d", sendBuffer, switchdata->backSwitch, switchdata->magnetSwitch);
  241. sprintf(sendBuffer, "%s%06d|%06d|%06d", sendBuffer, mpu6050data->ax, mpu6050data->ay, mpu6050data->az);
  242. Serial.printf("%s", sendBuffer);
  243. Serial.println();
  244. Udp.write(sendBuffer);
  245. Udp.endPacket();
  246. }
  247. void writeEeprom(char *WifiSSID, char *WifiPassword){
  248. //Clean memory
  249. clearEeprom();
  250. //Write ssid
  251. for(int i = 0; i < 17; i++){
  252. EEPROM.write(i, WifiSSID[i]);
  253. }
  254. //Write password
  255. for(int i = 0; i < 10; i++){
  256. EEPROM.write(17+i, WifiPassword[i]);
  257. }
  258. EEPROM.commit();
  259. }
  260. void clearEeprom(){
  261. for (int i = 0; i < 30; i++) { EEPROM.write(i, 0); }
  262. }