osc.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <OSCMessage.h>
  2. #include <Ethernet.h>
  3. #include <EthernetUdp.h>
  4. #include <SPI.h>
  5. #include <OSCBoards.h>
  6. #ifdef BOARD_HAS_USB_SERIAL
  7. #include <SLIPEncodedUSBSerial.h>
  8. SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
  9. #else
  10. #include <SLIPEncodedSerial.h>
  11. SLIPEncodedSerial SLIPSerial(Serial);
  12. #endif
  13. bool update_stepper_status[NUM_OF_STEPPERS];
  14. long int last_updated_status ;
  15. int update_ms = 1000 ;
  16. //// UTILITIES /////////
  17. template <typename TYPE> void sendOSC(const char * adress, int first_param, TYPE parameter) {
  18. OSCMessage OSCmsg(adress);
  19. OSCmsg.add(first_param);
  20. OSCmsg.add(parameter);
  21. SLIPSerial.beginPacket();
  22. //Udp.beginPacket(outIp, outPort);
  23. OSCmsg.send(SLIPSerial); // send the bytes to the SLIP stream
  24. // OSCmsg.send(Udp); // send the bytes to the SLIP stream
  25. SLIPSerial.endPacket();
  26. //Udp.endPacket(); // mark the end of the OSC Packet
  27. OSCmsg.empty(); // free space occupied by message
  28. }
  29. void printTest(OSCMessage &msg) {
  30. sendOSC("/still", 3, "up and running");
  31. }
  32. ///// STEPPER /////////
  33. void homing_Stepper_OSC(OSCMessage &msg) {
  34. homing_steppers();
  35. sendOSC("/homing",3, "done" );
  36. }
  37. void setCurrentPosition_Stepper_OSC (OSCMessage &msg) {
  38. int index = msg.getInt(0);
  39. steppers[index].setCurrentPosition(msg.getInt(1));
  40. sendOSC("/setPosition/", index, "position set");
  41. }
  42. void drivingMode_Stepper_OSC (OSCMessage &msg) {
  43. driveMode = msg.getInt(0);
  44. sendOSC("/driveMode/", 3, "set to " + driveMode);
  45. }
  46. void moveTo_Stepper_OSC (OSCMessage &msg){
  47. int index = msg.getInt(0);
  48. long int distance = moveTo_Stepper(index, msg.getInt(1));
  49. sendOSC("/distanceToGo", index, distance );
  50. // while (!stepper.distanceToGo()==0)
  51. // stepper.run();
  52. }
  53. void move_Stepper_OSC (OSCMessage &msg){
  54. int index = msg.getInt(0);
  55. long int distance = move_Stepper(index, msg.getInt(1));
  56. sendOSC("/distanceToGo", index, distance );
  57. }
  58. void setSpeed_Stepper (OSCMessage &msg){
  59. int index = msg.getInt(0);
  60. steppers[index].setSpeed(msg.getInt(1));
  61. sendOSC("/speed", index, steppers[index].speed() );
  62. }
  63. void maxSpeed_Stepper (OSCMessage &msg){
  64. int index = msg.getInt(0);
  65. long int max_speed = maxSpeed_Stepper(index, msg.getInt(1));
  66. sendOSC("/maxSpeed", index, max_speed );
  67. }
  68. void acceleration_Stepper (OSCMessage &msg){
  69. int index = msg.getInt(0);
  70. long int acceleration = acceleration_Stepper(index, msg.getInt(1));
  71. sendOSC("/acceleration", index, acceleration );
  72. }
  73. void currentPos_Stepper_OSC(OSCMessage &msg) {
  74. int index = msg.getInt(0);
  75. sendOSC("/currentPos", index, steppers[index].currentPosition());
  76. }
  77. void status_Stepper_OSC(int stepper_index) {
  78. OSCMessage OSCmsg("/status");
  79. OSCmsg.add(stepper_index);
  80. OSCmsg.add(steppers[stepper_index].currentPosition());
  81. //OSCmsg.add(steppers[stepper_index].targetPosition());
  82. OSCmsg.add(steppers[stepper_index].speed());
  83. //OSCmsg.add(steppers[stepper_index].maxSpeed());
  84. SLIPSerial.beginPacket();
  85. //Udp.beginPacket(outIp, outPort);
  86. OSCmsg.send(SLIPSerial); // send the bytes to the SLIP stream
  87. // OSCmsg.send(Udp); // send the bytes to the SLIP stream
  88. SLIPSerial.endPacket();
  89. //Udp.endPacket(); // mark the end of the OSC Packet
  90. OSCmsg.empty(); // free space occupied by message
  91. }
  92. void getStatus_stepper_OSC(OSCMessage &msg) {
  93. status_Stepper_OSC(msg.getInt(0)) ;
  94. }
  95. ////// OSC DECLARATIONS /////////
  96. void setup_OSC(){
  97. SLIPSerial.begin(115200);
  98. // delay (100);
  99. sendOSC("/ready", 3, "to go");
  100. }
  101. void handleOSCIn() {
  102. //send stepper status while it is running
  103. if (millis() - last_updated_status > update_ms ) {
  104. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  105. if (update_stepper_status[i]) {
  106. status_Stepper_OSC(i) ;
  107. }
  108. if (!steppers[i].isRunning()) { update_stepper_status[i] = 0 ;}
  109. else update_stepper_status[i] = 1 ;
  110. }
  111. last_updated_status = millis() ;
  112. }
  113. //send stepper status while it is running
  114. if ((millis() - last_updated_status > update_ms) && update_stepper_status[0] ) {
  115. status_Stepper_OSC(0) ;
  116. if (!steppers[0].isRunning()) { update_stepper_status[0] = 0 ;}
  117. else update_stepper_status[0] = 1 ;
  118. last_updated_status = millis() ;
  119. }
  120. if ((millis() - last_updated_status > update_ms*1.5) && update_stepper_status[1] ) {
  121. status_Stepper_OSC(1) ;
  122. if (!steppers[1].isRunning()) { update_stepper_status[1] = 0 ;}
  123. else update_stepper_status[1] = 1 ;
  124. last_updated_status = millis() ;
  125. }
  126. //define OSC message to function relationships
  127. OSCMessage OSCin;
  128. int size;
  129. if (SLIPSerial.available()) {
  130. while (!SLIPSerial.endofPacket())
  131. if ( (size = SLIPSerial.available()) > 0)
  132. {
  133. while (size--)
  134. OSCin.fill(SLIPSerial.read());
  135. }
  136. }
  137. //Declare valid OSC messages here
  138. if (!OSCin.hasError()) {
  139. OSCin.dispatch("/test", printTest);
  140. // //STEPPER CONTROL
  141. // OSCin.dispatch("/init", init_Stepper_OSC);
  142. OSCin.dispatch("/getStatus", getStatus_stepper_OSC);
  143. OSCin.dispatch("/setCurrentPosition", setCurrentPosition_Stepper_OSC);
  144. OSCin.dispatch("/currentPosition", currentPos_Stepper_OSC);
  145. OSCin.dispatch("/driveMode", drivingMode_Stepper_OSC);
  146. OSCin.dispatch("/moveTo", moveTo_Stepper_OSC);
  147. OSCin.dispatch("/move", move_Stepper_OSC);
  148. // OSCin.dispatch("/moveTo/cm", moveToCm_Stepper_OSC);
  149. // OSCin.dispatch("/move/cm", moveCm_Stepper_OSC);
  150. OSCin.dispatch("/setSpeed", setSpeed_Stepper);
  151. OSCin.dispatch("/maxSpeed", maxSpeed_Stepper);
  152. OSCin.dispatch("/acceleration", acceleration_Stepper);
  153. // OSCin.dispatch("/randomMove", randomMove_Stepper_OSC);
  154. //
  155. // //LEDS
  156. // OSCin.dispatch("/led/value", valueLed_OSC);
  157. // OSCin.dispatch("/led/sensor", sensorLed_OSC);
  158. // OSCin.dispatch("/led/lighting", lightingLed_OSC);
  159. }
  160. // }
  161. }