diff --git a/2019-teensy-nrf24/remote/Joysticks.h b/2019-teensy-nrf24/remote/Joysticks.h new file mode 100644 index 0000000..61445e5 --- /dev/null +++ b/2019-teensy-nrf24/remote/Joysticks.h @@ -0,0 +1,127 @@ +#include + +// start reading from the first byte (address 0) of the EEPROM +unsigned int address = 0; +byte value; + +// Joystick vars +const int analogInPin = A0; + +int sensorValue = 0; +int outputValue = 0; + +//volatile int center[6] = {0, 0, 0, 0, 0, 0}; +//volatile int minvalue[6] = {1000, 1000, 1000, 1000, 1000, 1000}; //ARDUINO +//volatile int maxvalue[6] = {0, 0, 0, 0, 0, 0}; //ARDUINO +int minvalue[6] = {1000, 1000, 1000, 1000, 1000, 1000}; //TEENSY +int maxvalue[6] = {0, 0, 0, 0, 0, 0}; //TEENSY + +void JoystickInit() +{ + // Initialize joysticks first + value = EEPROM.read(address); + + if (value != 0) + { + address++; + for (int i = 0; i < 6; i++) + { + address += sizeof(int); + EEPROM.get(address, minvalue[i]); + address += sizeof(int); + EEPROM.get(address, maxvalue[i]); + // Serial.println(minvalue[i]); + // Serial.println(maxvalue[i]); + } + } + else + { + // if eeprom empty + delay(500); + + // for (int i = 0; i < 6; i++) + // { + // center[i] = analogRead(analogInPin + i); + // } + + int time = 10000; + while (millis() < time) + { + for (int i = 0; i < 6; i++) + { + sensorValue = analogRead(analogInPin + i); + if (sensorValue > maxvalue[i]) { + maxvalue[i] = sensorValue; + } + if (sensorValue < minvalue[i]) { + minvalue[i] = sensorValue; + } + } + } + + // for (int i = 0; i < 6; i++) + // { + // minvalue[i] = center[i] - minvalue[i]; + // maxvalue[i] = (maxvalue[i] - center[i]); + // } + + // write callibration to eeprom + address = 0; + EEPROM.write(address, 1); + + address++; + for (int i = 0; i < 6; i++) + { + address += sizeof(int); + EEPROM.put(address, minvalue[i]); + address += sizeof(int); + EEPROM.put(address, maxvalue[i]); + // Serial.println(minvalue[i]); + // Serial.println(maxvalue[i]); + } + } +} + +struct joyStickdataStruct { + float value[6]; +} myJoyStickdataStruct; + +joyStickdataStruct JoystickGet(bool debug = false) +{ + for (int i = 0; i < 6; i++) + { + sensorValue = analogRead(analogInPin + i); + + if (sensorValue > maxvalue[i]) + maxvalue[i] = sensorValue; + if (sensorValue < minvalue[i]) + minvalue[i] = sensorValue; + + outputValue = map(sensorValue, minvalue[i], maxvalue[i], 0, 255); + outputValue = sensorValue > maxvalue[i] ? 255 : sensorValue < minvalue[i] ? 0 : outputValue; + // if (sensorValue <= center[i]) + // { + // outputValue = map(sensorValue, minvalue[i], center[i], 0, 127); + // } + // else + // { + // outputValue = map(sensorValue, center[i], maxvalue[i], 128, 255); + // } + + myJoyStickdataStruct.value[i] = outputValue; + + if (debug) + { + Serial.print(i); + Serial.print("_"); + Serial.print(myJoyStickdataStruct.value[i]); + if (i != 5) + Serial.print(","); + // Serial.print(" "); + } + } + if (debug) + Serial.println(); + + return myJoyStickdataStruct; +} diff --git a/2019-teensy-nrf24/remote/Nrf.h b/2019-teensy-nrf24/remote/Nrf.h new file mode 100644 index 0000000..f71bae6 --- /dev/null +++ b/2019-teensy-nrf24/remote/Nrf.h @@ -0,0 +1,64 @@ +#include +#include "RF24.h" +#include +#include "TransferDataStructure.h" + +// Nrf24 settings +RF24 radio(9, 10); // CE, CSN +RF24Network network(radio); + +const uint16_t this_node = 00; // Address of our node in Octal format +const uint16_t other_node = 01; // Address of the other node in Octal format + +void NrfInit() +{ + SPI.begin(); + radio.begin(); + network.begin(/*channel*/ 90, /*node address*/ this_node); + + // Set the PA Level low to prevent power supply related issues since this is a + // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default. + radio.setPALevel(RF24_PA_LOW); +} + +bool NrfSendData(dataStruct2 data, bool debug = false) +//dataStruct2 NrfSendData(dataStruct2 data, bool debug = false) +{ + // network.update(); + + if (debug) + Serial.print("Sending..."); + + RF24NetworkHeader header(/*to node*/ other_node); + bool ok = network.write(header, &data, sizeof(data)); + + if (debug) + { + if (ok) + Serial.println("ok."); + else + Serial.println("failed."); + } + + return ok; + // return data; +} + +dataStruct2 NrfGetData(dataStruct2 data, bool debug = false) +{ + // network.update(); + int timeout = millis() + 100; + while ( network.available() && timeout > millis() ) + { + RF24NetworkHeader header; + network.read(header, &data, sizeof(data)); + + if (debug) + { + Serial.print("Received packet "); + Serial.println(data.servoPositions[0]); + } + } + + return data; +} diff --git a/2019-teensy-nrf24/remote/README.md b/2019-teensy-nrf24/remote/README.md new file mode 100644 index 0000000..c0869a6 --- /dev/null +++ b/2019-teensy-nrf24/remote/README.md @@ -0,0 +1,3 @@ +# remote + +arduino code for remote control \ No newline at end of file diff --git a/2019-teensy-nrf24/remote/SerialPasstrough.h b/2019-teensy-nrf24/remote/SerialPasstrough.h new file mode 100644 index 0000000..54f2b60 --- /dev/null +++ b/2019-teensy-nrf24/remote/SerialPasstrough.h @@ -0,0 +1,122 @@ +#include "TransferDataStructure.h" + +String getValue(String data, char separator, int index) +{ + int found = 0; + int strIndex[] = { 0, -1 }; + int maxIndex = data.length() - 1; + + for (int i = 0; i <= maxIndex && found <= index; i++) { + if (data.charAt(i) == separator || i == maxIndex) { + found++; + strIndex[0] = strIndex[1] + 1; + strIndex[1] = (i == maxIndex) ? i + 1 : i; + } + } + return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; +} + +dataStruct2 GetSerialData(dataStruct2 &dataIn, bool debug = false) +{ + if (Serial.available() && Serial.peek() != -1) + { + // String data = Serial.readStringUntil('\r'); + // while (Serial.read() >= 0); + // int maxIndex = data.length() - 1; + // int index = 0; + // int next_index; + // String data_word; + // String channel; + // String angle; + // do { + // next_index = data.indexOf(',', index); + // + // data_word = data.substring(index, next_index); + // channel = data_word.substring(0, data_word.indexOf('_')); + // angle = data_word.substring(data_word.indexOf('_') + 1); + // + // if (isDigit(angle[0])) // prevent error overwriting first entry. + // dataIn.servoPositions[channel.toInt()] = angle.toFloat(); + // + // index = next_index + 1; + // } while ((next_index != -1) && (next_index < maxIndex)); + + + /*String s = Serial.readStringUntil('s'); + for (int i = 0; i < 18; i++) + { + Serial.setTimeout(10); + String c = Serial.readStringUntil('_'); + String v = Serial.readStringUntil(','); + String cc = Serial.readStringUntil('='); + int channel = c.toInt(); + int value = v.toInt(); + int crc = cc.toInt(); + + if ((crc == (channel) * value) && value >= 0 && value <= 180) + dataIn.servoPositions[channel] = value;*/ + //byte recv[18 + 1]; + if (Serial.available() >= 18 + 1 + 2 + 30) + { + bool recc = false; + byte recv[256] = {0}; + //Serial.readBytesUntil(0xffffff, recv, 255); + Serial.readBytesUntil(0xff, recv, 255); + for (int i = 0; i < 255 - 19; i++) + { + // if (recv[i + 1] == 0xff && recv[i + 2] == 0xff && recv[i + 3] == 0xff) + if (recv[i + 19] == 0xfe) + { + for (int j = 0; j < 18 + 1; j++) + { + recv[j] = recv[(i) + j]; + } + recc = true; + + break; + } + } + + + //s.getBytes(recv, s.length()); + byte crc = 0x00; + for (int i = 0; i < 18; i++) + { + crc += recv[i]; + } + + + if (crc == recv[18]) + { + + if (recc) + { + for (int i = 0; i < 18; i++) + { + dataIn.servoPositions[i] = recv[i]; + } + } + } + //} + + dataIn.remotePasstroughControl = true; + + if (debug) + { + Serial.println("Enabled Servo Passtrough mode!"); + + Serial.println("Output line: "); + for (int i = 0; i < 18; i++) + { + Serial.print(i); + Serial.print(", "); + Serial.print(dataIn.servoPositions[i]); + Serial.println(""); + } + Serial.println(""); + } + } + } + + return dataIn; +} diff --git a/2019-teensy-nrf24/remote/TransferDataStructure.h b/2019-teensy-nrf24/remote/TransferDataStructure.h new file mode 100644 index 0000000..0b3daea --- /dev/null +++ b/2019-teensy-nrf24/remote/TransferDataStructure.h @@ -0,0 +1,38 @@ +#ifndef dataStruct +#define dataStruct + +struct __attribute__((__packed__)) dataStruct2 +{ + bool STOP = false; + + float walkSpeed = 10; //step size + float walkSpeed2 = 0; //gate speed + float walkRotation = 0 ; + float walkDirection = 0; + + float tranform[3] = {0, 0, 0}; + float tranform2[3] = {0, 0, 0}; + float rotation[3] = {0, 0, 0}; + + bool remotePasstroughControl = 0; + float servoPositions[18] = { + 90, 90, 90, + 90, 90, 90, + 90, 90, 90, + + 90, 90, 90, + 90, 90, 90, + 90, 90, 90 + }; +} dataStruct; + + +//template struct print_ct { +// print_ct() { +// static_assert(sizeof...(T) == -1, ""); +// } +//}; +// +//print_ct< sizeof(dataStruct2), alignof(dataStruct2) > v; + +#endif diff --git a/2019-teensy-nrf24/remote/remote.ino b/2019-teensy-nrf24/remote/remote.ino new file mode 100644 index 0000000..5656473 --- /dev/null +++ b/2019-teensy-nrf24/remote/remote.ino @@ -0,0 +1,176 @@ +#include "Joysticks.h" +#include "Nrf.h" +#include "SerialPasstrough.h" +#include +#include +#include "Nextion.h" + +NexPage page0 = NexPage(4, 0, "uploadscreen"); + +NexText t0 = NexText(1, 6, "t1"); +NexText t1 = NexText(3, 22, "t1"); + +NexButton b2 = NexButton(1, 5, "b2"); +NexSlider sliders[18] = {NexSlider(3, 1, "h0"), + NexSlider(3, 2, "h1"), + NexSlider(3, 3, "h2"), + NexSlider(3, 4, "h3"), + NexSlider(3, 5, "h4"), + NexSlider(3, 6, "h5"), + NexSlider(3, 7, "h6"), + NexSlider(3, 8, "h7"), + NexSlider(3, 9, "h8"), + NexSlider(3, 10, "h9"), + NexSlider(3, 11, "h10"), + NexSlider(3, 12, "h11"), + NexSlider(3, 13, "h12"), + NexSlider(3, 14, "h13"), + NexSlider(3, 15, "h14"), + NexSlider(3, 16, "h15"), + NexSlider(3, 17, "h16"), + NexSlider(3, 18, "h17") + }; + +char buffer[100] = {0}; + +NexTouch *nex_listen_list[] = +{ + &b2, + &page0, + &t0, + &t1, + NULL +}; + +void b2PopCallback(void *ptr) +{ + passtrough(); +} + +MPU6050 mpu (Wire); + +dataStruct2 data; +joyStickdataStruct joystickData; + +void setup() { +// Serial.begin(250000, SERIAL_8E1); // ARDUINO + Serial.begin(250000); // TEENSY + // Serial.begin(115200); + Serial1.begin(115200); + // passtrough(); + + nexInit(); + b2.attachPush(b2PopCallback, &b2); + b2.attachPop(b2PopCallback, &b2); + // for (int i = 0; i < 18; i++) + // sliders[i] = NexSlider(3, i++, "h" + i); + // while (true) + // { + // nexLoop(nex_listen_list); + // sliders[0].setValue(30); + // } + + // Initialization + // Wire.begin(); + // mpu.begin(); + // + // mpu.calcGyroOffsets(true, 300, 1000); + // + // // Calibration + // Serial.println("====================================="); + // Serial.println("Starting calibration..."); + // mpu.calcGyroOffsets(); + // Serial.println("Calibration complete!"); + + JoystickInit(); + + NrfInit(); +} + +void passtrough() +{ + delay(100); + for (int i = 0; i < 10; i++) + { + // nexLoop(nex_listen_list); + // delay(10); + page0.show(); + delay(10); + } + + Serial.begin(115200); + Serial1.begin(115200); + do { + if (Serial.available()) { // If anything comes in Serial (USB), + Serial1.write(Serial.read()); // read it and send it out Serial1 (pins 0 & 1) + } + + if (Serial1.available()) { // If anything comes in Serial1 (pins 0 & 1) + Serial.write(Serial1.read()); // read it and send it out Serial (USB) + } + } while (true); +} + +void loop() +{ + nexLoop(nex_listen_list); + network.update(); + + if (false) + { + mpu.update(); + Serial.print("AngX = "); + Serial.print(mpu.getAngleX()); + Serial.print(" / AngY = "); + Serial.print(mpu.getAngleY()); + Serial.print(" / AngZ = "); + Serial.println(mpu.getAngleZ()); + } + + // Serial.print(" / AngX = "); + // Serial.print(mpu.getGyroAngleX()); + // Serial.print(" / AngY = "); + // Serial.print(mpu.getGyroAngleY()); + // Serial.print(" / AngZ = "); + // Serial.println(mpu.getGyroAngleZ()); + + joystickData = JoystickGet(true); + + // test + if (false) + { + data.remotePasstroughControl = true; + data.servoPositions[1] = (joystickData.value[2] * 0.78f); + data.servoPositions[4] = (joystickData.value[2] * 0.78f); + data.servoPositions[7] = (joystickData.value[2] * 0.78f); + + data.servoPositions[10] = 180 - (joystickData.value[2] * 0.78f); + data.servoPositions[13] = 180 - (joystickData.value[2] * 0.78f); + data.servoPositions[16] = 180 - (joystickData.value[2] * 0.78f); + } + + GetSerialData(data, false); + // data = GetSerialData(data, false); + + // data.STOP = true; + + for (int i = 0; i < 18; i++) + sliders[i].setValue(data.servoPositions[i]); + + bool ok = NrfSendData(data, false); + // data = NrfSendData(data, false); + + if (ok) + t0.setText("connected"); + else + t0.setText("not connected"); + if (ok) + t1.setText("connected"); + else + t1.setText("not connected"); + + //test + // data = NrfGetData(data, true); + + delay(20); +}