Controller.ino 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. }
  155. void readJoystick(struct JoystickData *joystickdata){
  156. joystickdata->x = ads.readADC_SingleEnded(1, 0) - joystickoffset_x;
  157. delay(8);
  158. joystickdata->y = ads.readADC_SingleEnded(0, 0) - joystickoffset_y;
  159. }
  160. void readSwitches(struct SwitchData *switchdata){
  161. switchdata->magnetSwitch = !digitalRead(12);
  162. }
  163. void rumble(int duration, int power){
  164. rumbleActivated = true;
  165. analogWrite(15, power);
  166. rumbleDuration = millis() + duration;
  167. }
  168. void searchBasestation(void){
  169. Serial.print("Scanning for basestation ");
  170. for(int j = 0; j < 10; j++){
  171. int n = WiFi.scanNetworks();
  172. for (int i = 0; i < n; ++i){
  173. if(WiFi.SSID(i).startsWith(WifiSSIDPrefix)){ //We've found a basestation
  174. char WifiPassword[10];
  175. char WifiSSID[17];
  176. WiFi.SSID(i).toCharArray(WifiSSID, WiFi.SSID(i).length() + 1);
  177. calculateWiFiPassword(WiFi.SSID(i), WifiPassword);
  178. Serial.printf("Basestation found! SSID: %s", WifiSSID);
  179. Serial.printf("\nBasestation password: %s", WifiPassword);
  180. //Connect to the basestation
  181. WiFi.begin(WifiSSID, WifiPassword);
  182. while (WiFi.status() != WL_CONNECTED) {
  183. delay(500);
  184. Serial.print(".");
  185. }
  186. ///Writing data to eeprom
  187. writeEeprom(WifiSSID, WifiPassword);
  188. //Break out of the loop and stop scanning
  189. n = 254;
  190. j = 254;
  191. }
  192. }
  193. Serial.print(".");
  194. delay(1000);
  195. }
  196. }
  197. void connectLastBasestation(void){
  198. Serial.println("Reading information from EEPROM");
  199. char eepromWifiSSID[17];
  200. char eepromWifiPassword[10];
  201. //Read SSID
  202. for (int i = 0; i < 17; i++){
  203. eepromWifiSSID[i] = char(EEPROM.read(i));
  204. }
  205. eepromWifiSSID[17] = '\0';
  206. //Read Password
  207. for (int i = 0; i < 10; i++){
  208. eepromWifiPassword[i]= char(EEPROM.read(17+i));
  209. }
  210. eepromWifiPassword[10] = '\0';
  211. //Verify valid memory
  212. if(String(eepromWifiSSID).startsWith(WifiSSIDPrefix)){
  213. Serial.printf("Valid basestation information in eeprom found! \nEeprom SSID: %s \nEeprom Password: %s", eepromWifiSSID, eepromWifiPassword);
  214. //Connect to the basestation
  215. WiFi.begin(eepromWifiSSID, eepromWifiPassword);
  216. while (WiFi.status() != WL_CONNECTED) {
  217. delay(500);
  218. Serial.print(".");
  219. }
  220. }
  221. }
  222. void calculateWiFiPassword(String WifiSSID, char *WifiPassword)
  223. {
  224. String WifiStringPassword;
  225. for(char i=0; i < 17; i++){
  226. String s = String(WifiSSID[i], HEX);
  227. WifiStringPassword = s + WifiStringPassword;
  228. }
  229. for (int i=0; i < 10; i++)
  230. WifiPassword[i] = WifiStringPassword.charAt(i);
  231. WifiPassword[9] = '\0';
  232. }
  233. void sendUdpMessage(struct JoystickData *joystick, struct MPU6050Data *mpu6050data, struct SwitchData *switchdata){
  234. Udp.beginPacket(BasestationIp, BasestationPort);
  235. sprintf(sendBuffer, "%06d|%06d|%01d|", joystick->x, joystick->y, joystick->button);
  236. sprintf(sendBuffer, "%s%06d|%06d|%06d|", sendBuffer, mpu6050data->yaw, mpu6050data->pitch, mpu6050data->roll);
  237. sprintf(sendBuffer, "%s%01d|%01d", sendBuffer, switchdata->backSwitch, switchdata->magnetSwitch);
  238. Serial.printf("%s", sendBuffer);
  239. Serial.println();
  240. Udp.write(sendBuffer);
  241. Udp.endPacket();
  242. }
  243. void writeEeprom(char *WifiSSID, char *WifiPassword){
  244. //Clean memory
  245. clearEeprom();
  246. //Write ssid
  247. for(int i = 0; i < 17; i++){
  248. EEPROM.write(i, WifiSSID[i]);
  249. }
  250. //Write password
  251. for(int i = 0; i < 10; i++){
  252. EEPROM.write(17+i, WifiPassword[i]);
  253. }
  254. EEPROM.commit();
  255. }
  256. void clearEeprom(){
  257. for (int i = 0; i < 30; i++) { EEPROM.write(i, 0); }
  258. }