Touch OSC and NodeMCU

1) A layout for the mobile phone
2) A sketch to receive the data on a NodeMCU over Wifi
3) A sketch to send a RC package to the stm32 from the NodeMCU

TODO: Merge these two sketches later
This commit is contained in:
philsson 2018-09-17 21:20:29 +02:00 committed by Philip Johansson
parent a1cb640b2b
commit 9302eadae7
4 changed files with 233 additions and 4 deletions

View File

@ -0,0 +1,183 @@
/*---------------------------------------------------------------------------------------------
Open Sound Control (OSC) library for the ESP8266/ESP32
Example for receiving open sound control (OSC) bundles on the ESP8266/ESP32
Send integers '0' or '1' to the address "/led" to turn on/off the built-in LED of the esp8266.
This example code is in the public domain.
Inspired from https://trippylighting.com/teensy-arduino-ect/touchosc-and-arduino-oscuino/
--------------------------------------------------------------------------------------------- */
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>
char ssid[] = "FaRgO2G4"; // your network SSID (name)
char pass[] = "Johansson85"; // your network password
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
const IPAddress outIp(192,168,1,72); // remote IP (not needed for receive)
const unsigned int outPort = 9999; // remote port (not needed for receive)
const unsigned int localPort = 8888; // local port to listen for UDP packets (here's where we send the packets)
OSCErrorCode error;
unsigned int ledState = LOW; // LOW means led is *on*
#ifndef BUILTIN_LED
#ifdef LED_BUILTIN
#define BUILTIN_LED LED_BUILTIN
#else
#define BUILTIN_LED 13
#endif
#endif
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, ledState); // turn *on* led
Serial.begin(115200);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
#ifdef ESP32
Serial.println(localPort);
#else
Serial.println(Udp.localPort());
#endif
}
void OSCMsgReceive(){
OSCMessage msgIN;
int size;
if((size = Udp.parsePacket())>0){
while(size--)
msgIN.fill(Udp.read());
if(!msgIN.hasError()){
msgIN.route("/Robot/Enable",toggleEnabled);
msgIN.route("/Fader/Throttle",funcThrottle);
msgIN.route("/Fader/Steering",funcSteering);
msgIN.route("/Gain/Kp",funcKp);
//msgIN.route("/Gain/Ki",funcKi);
//msgIN.route("/Gain/Kd",funcKd);
}
}
}
void toggleEnabled(OSCMessage &msg, int addrOffset){
ledState = (boolean) msg.getFloat(0);
OSCMessage msgOUT("/Robot/Enable");
digitalWrite(BUILTIN_LED, !ledState);
msgOUT.add(ledState);
if (ledState) {
Serial.println("Robot enabled");
}
else {
Serial.println("Robot disabled");
}
//send osc message back to controll object in TouchOSC
//Local feedback is turned off in the TouchOSC interface.
//The button is turned on in TouchOSC interface whe the conrol receives this message.
Udp.beginPacket(Udp.remoteIP(), outPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void funcThrottle(OSCMessage &msg, int addrOffset ){
int value = msg.getFloat(0);
OSCMessage msgOUT("/Fader/Throttle");
Serial.print("Throttle = : ");
Serial.println(value);
msgOUT.add(value);
Udp.beginPacket(Udp.remoteIP(), outPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void funcSteering(OSCMessage &msg, int addrOffset ){
int value = msg.getFloat(0);
OSCMessage msgOUT("/Fader/Steering");
Serial.print("Steering = : ");
Serial.println(value);
msgOUT.add(value);
Udp.beginPacket(Udp.remoteIP(), outPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void funcKp(OSCMessage &msg, int addrOffset ){
int value = msg.getFloat(0);
OSCMessage msgOUT("/Gain/Kp");
Serial.print("Kp = : ");
Serial.println(value);
msgOUT.add(value);
Udp.beginPacket(Udp.remoteIP(), outPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
// Redo this for label
OSCMessage msgLABEL("/Gain/KpOut");
msgLABEL.add(value);
Udp.beginPacket(Udp.remoteIP(), outPort);
msgLABEL.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgLABEL.empty(); // free space occupied by message
}
void loop() {
OSCMsgReceive();
}

View File

@ -0,0 +1,50 @@
unsigned int ledState = LOW;
typedef struct Packet {
int16_t MagicWordLow;
int16_t MagicWordHigh;
int16_t Throttle;
int16_t Steering;
int16_t Kp;
int16_t Ki;
int16_t Kd;
bool Enabled;
};
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(57600);
Serial.println("Starting...");
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
ledState = !ledState;
digitalWrite(BUILTIN_LED, ledState);
int bufferSize = 15;
uint8_t buffer[bufferSize];
Packet* pPacket = (Packet*)buffer;
pPacket->MagicWordLow = 0x0DED;
pPacket->MagicWordHigh = 0x0DEC;
pPacket->Throttle = 123;
pPacket->Steering = 456;
pPacket->Kp = 10;
pPacket->Ki = 20;
pPacket->Kd = 30;
pPacket->Enabled = true;
Serial.write(buffer, bufferSize);
}

BIN
mobile/Robot.touchosc Normal file

Binary file not shown.

View File

@ -24,8 +24,6 @@ public:
RCProtocol();
void start();
//! Read the latest package
RCProtocol::Packet read();
@ -33,8 +31,6 @@ public:
//! Returns true when a hole package is available
bool appendByte(uint8_t newByte);
// static int size();
const static uint32_t MAGIC_WORD = 0xDEC0DED;
private: