123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #include <OSCMessage.h>
- #include <Ethernet.h>
- #include <EthernetUdp.h>
- #include <SPI.h>
- #include <OSCBoards.h>
- #ifdef BOARD_HAS_USB_SERIAL
- #include <SLIPEncodedUSBSerial.h>
- SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
- #else
- #include <SLIPEncodedSerial.h>
- SLIPEncodedSerial SLIPSerial(Serial);
- #endif
- bool update_stepper_status[NUM_OF_STEPPERS];
- //// UTILITIES /////////
- template <typename TYPE> void sendOSC(const char * adress, int first_param, TYPE parameter) {
- OSCMessage OSCmsg(adress);
- OSCmsg.add(first_param);
- OSCmsg.add(parameter);
- SLIPSerial.beginPacket();
- //Udp.beginPacket(outIp, outPort);
- OSCmsg.send(SLIPSerial); // send the bytes to the SLIP stream
- // OSCmsg.send(Udp); // send the bytes to the SLIP stream
- SLIPSerial.endPacket();
- //Udp.endPacket(); // mark the end of the OSC Packet
- OSCmsg.empty(); // free space occupied by message
- }
- void printTest(OSCMessage &msg) {
- sendOSC("/still", 3, "up and running");
- }
- ///// STEPPER /////////
- //void init_Stepper_OSC(OSCMessage &msg) {
- // float maximum = init_Stepper();
- // sendOSC("/stepper/maxPos",maximum );
- //}
- //
- void setCurrentPosition_Stepper_OSC (OSCMessage &msg) {
- int index = msg.getInt(0);
- steppers[index].setCurrentPosition(msg.getInt(1));
- sendOSC("/stepper/setPosition/", index, "position set");
- }
- void drivingMode_Stepper_OSC (OSCMessage &msg) {
- driveMode = msg.getInt(0);
- sendOSC("/stepper/driveMode/", 3, "set to " + driveMode);
- }
- void moveTo_Stepper_OSC (OSCMessage &msg){
- int index = msg.getInt(0);
- long int distance = moveTo_Stepper(index, msg.getInt(1));
- sendOSC("/stepper/distanceToGo", index, distance );
- // while (!stepper.distanceToGo()==0)
- // stepper.run();
- }
- void move_Stepper_OSC (OSCMessage &msg){
- int index = msg.getInt(0);
- long int distance = move_Stepper(index, msg.getInt(1));
- sendOSC("/stepper/distanceToGo", index, distance );
- }
- void maxSpeed_Stepper (OSCMessage &msg){
- int index = msg.getInt(0);
- long int max_speed = maxSpeed_Stepper(index, msg.getInt(1));
- sendOSC("/stepper/maxSpeed", index, max_speed );
- }
- void acceleration_Stepper (OSCMessage &msg){
- int index = msg.getInt(0);
- long int acceleration = acceleration_Stepper(index, msg.getInt(1));
- sendOSC("/stepper/acceleration", index, acceleration );
- }
- void currentPos_Stepper_OSC(OSCMessage &msg) {
- int index = msg.getInt(0);
- sendOSC("/stepper/currentPos", index, steppers[index].currentPosition());
- }
- void status_Stepper_OSC(int stepper_index) {
- OSCMessage OSCmsg("/stepper/status");
- OSCmsg.add(stepper_index);
- OSCmsg.add(steppers[stepper_index].currentPosition());
- OSCmsg.add(steppers[stepper_index].targetPosition());
- OSCmsg.add(steppers[stepper_index].speed());
- OSCmsg.add(steppers[stepper_index].maxSpeed());
- SLIPSerial.beginPacket();
- //Udp.beginPacket(outIp, outPort);
- OSCmsg.send(SLIPSerial); // send the bytes to the SLIP stream
- // OSCmsg.send(Udp); // send the bytes to the SLIP stream
- SLIPSerial.endPacket();
- //Udp.endPacket(); // mark the end of the OSC Packet
- OSCmsg.empty(); // free space occupied by message
- }
- void getStatus_stepper_OSC(OSCMessage &msg) {
- status_Stepper_OSC(msg.getInt(0)) ;
- }
- ////// OSC DECLARATIONS /////////
- void setup_OSC(){
-
- SLIPSerial.begin(115200);
- // delay (100);
- sendOSC("/ready", 3, "to go");
-
- }
- void handleOSCIn() {
- //send stepper status while it is running
- for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
- if (update_stepper_status[i]) {
- status_Stepper_OSC(i) ;
- }
- if (!steppers[i].isRunning()) { update_stepper_status[i] = 0 ;}
- else update_stepper_status[i] = 1 ;
- }
- //define OSC message to function relationships
- OSCMessage OSCin;
- int size;
- if (SLIPSerial.available()) {
- while (!SLIPSerial.endofPacket())
- if ( (size = SLIPSerial.available()) > 0)
- {
- while (size--)
- OSCin.fill(SLIPSerial.read());
- }
- }
-
- //Declare valid OSC messages here
- if (!OSCin.hasError()) {
- OSCin.dispatch("/test", printTest);
- // //STEPPER CONTROL
- // OSCin.dispatch("/stepper/init", init_Stepper_OSC);
- OSCin.dispatch("/stepper/getStatus", getStatus_stepper_OSC);
- OSCin.dispatch("/stepper/setCurrentPosition", setCurrentPosition_Stepper_OSC);
- OSCin.dispatch("/stepper/currentPosition", currentPos_Stepper_OSC);
- OSCin.dispatch("/stepper/driveMode", drivingMode_Stepper_OSC);
- OSCin.dispatch("/stepper/moveTo", moveTo_Stepper_OSC);
- OSCin.dispatch("/stepper/move", move_Stepper_OSC);
- // OSCin.dispatch("/stepper/moveTo/cm", moveToCm_Stepper_OSC);
- // OSCin.dispatch("/stepper/move/cm", moveCm_Stepper_OSC);
- OSCin.dispatch("/stepper/maxSpeed", maxSpeed_Stepper);
- OSCin.dispatch("/stepper/acceleration", acceleration_Stepper);
- // OSCin.dispatch("/stepper/randomMove", randomMove_Stepper_OSC);
- //
- // //LEDS
- // OSCin.dispatch("/led/value", valueLed_OSC);
- // OSCin.dispatch("/led/sensor", sensorLed_OSC);
- // OSCin.dispatch("/led/lighting", lightingLed_OSC);
- }
- // }
- }
|