osc.h 5.9 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 init_Stepper_OSC(OSCMessage &msg) {
  34. // float maximum = init_Stepper();
  35. // sendOSC("/stepper/maxPos",maximum );
  36. //}
  37. //
  38. void setCurrentPosition_Stepper_OSC (OSCMessage &msg) {
  39. int index = msg.getInt(0);
  40. steppers[index].setCurrentPosition(msg.getInt(1));
  41. sendOSC("/stepper/setPosition/", index, "position set");
  42. }
  43. void drivingMode_Stepper_OSC (OSCMessage &msg) {
  44. driveMode = msg.getInt(0);
  45. sendOSC("/stepper/driveMode/", 3, "set to " + driveMode);
  46. }
  47. void moveTo_Stepper_OSC (OSCMessage &msg){
  48. int index = msg.getInt(0);
  49. long int distance = moveTo_Stepper(index, msg.getInt(1));
  50. sendOSC("/stepper/distanceToGo", index, distance );
  51. // while (!stepper.distanceToGo()==0)
  52. // stepper.run();
  53. }
  54. void move_Stepper_OSC (OSCMessage &msg){
  55. int index = msg.getInt(0);
  56. long int distance = move_Stepper(index, msg.getInt(1));
  57. sendOSC("/stepper/distanceToGo", index, distance );
  58. }
  59. void setSpeed_Stepper (OSCMessage &msg){
  60. int index = msg.getInt(0);
  61. steppers[index].setSpeed(msg.getInt(1));
  62. sendOSC("/stepper/speed", index, steppers[index].speed() );
  63. }
  64. void maxSpeed_Stepper (OSCMessage &msg){
  65. int index = msg.getInt(0);
  66. long int max_speed = maxSpeed_Stepper(index, msg.getInt(1));
  67. sendOSC("/stepper/maxSpeed", index, max_speed );
  68. }
  69. void acceleration_Stepper (OSCMessage &msg){
  70. int index = msg.getInt(0);
  71. long int acceleration = acceleration_Stepper(index, msg.getInt(1));
  72. sendOSC("/stepper/acceleration", index, acceleration );
  73. }
  74. void currentPos_Stepper_OSC(OSCMessage &msg) {
  75. int index = msg.getInt(0);
  76. sendOSC("/stepper/currentPos", index, steppers[index].currentPosition());
  77. }
  78. void status_Stepper_OSC(int stepper_index) {
  79. OSCMessage OSCmsg("/stepper/status");
  80. OSCmsg.add(stepper_index);
  81. OSCmsg.add(steppers[stepper_index].currentPosition());
  82. OSCmsg.add(steppers[stepper_index].targetPosition());
  83. OSCmsg.add(steppers[stepper_index].speed());
  84. OSCmsg.add(steppers[stepper_index].maxSpeed());
  85. SLIPSerial.beginPacket();
  86. //Udp.beginPacket(outIp, outPort);
  87. OSCmsg.send(SLIPSerial); // send the bytes to the SLIP stream
  88. // OSCmsg.send(Udp); // send the bytes to the SLIP stream
  89. SLIPSerial.endPacket();
  90. //Udp.endPacket(); // mark the end of the OSC Packet
  91. OSCmsg.empty(); // free space occupied by message
  92. }
  93. void getStatus_stepper_OSC(OSCMessage &msg) {
  94. status_Stepper_OSC(msg.getInt(0)) ;
  95. }
  96. ////// OSC DECLARATIONS /////////
  97. void setup_OSC(){
  98. SLIPSerial.begin(115200);
  99. // delay (100);
  100. sendOSC("/ready", 3, "to go");
  101. }
  102. void handleOSCIn() {
  103. //send stepper status while it is running
  104. if (millis() - last_updated_status > update_ms ) {
  105. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  106. if (update_stepper_status[i]) {
  107. status_Stepper_OSC(i) ;
  108. }
  109. if (!steppers[i].isRunning()) { update_stepper_status[i] = 0 ;}
  110. else update_stepper_status[i] = 1 ;
  111. }
  112. last_updated_status = millis() ;
  113. }
  114. //send stepper status while it is running
  115. if ((millis() - last_updated_status > update_ms) && update_stepper_status[0] ) {
  116. status_Stepper_OSC(0) ;
  117. if (!steppers[0].isRunning()) { update_stepper_status[0] = 0 ;}
  118. else update_stepper_status[0] = 1 ;
  119. last_updated_status = millis() ;
  120. }
  121. if ((millis() - last_updated_status > update_ms*1.5) && update_stepper_status[1] ) {
  122. status_Stepper_OSC(1) ;
  123. if (!steppers[1].isRunning()) { update_stepper_status[1] = 0 ;}
  124. else update_stepper_status[1] = 1 ;
  125. last_updated_status = millis() ;
  126. }
  127. //define OSC message to function relationships
  128. OSCMessage OSCin;
  129. int size;
  130. if (SLIPSerial.available()) {
  131. while (!SLIPSerial.endofPacket())
  132. if ( (size = SLIPSerial.available()) > 0)
  133. {
  134. while (size--)
  135. OSCin.fill(SLIPSerial.read());
  136. }
  137. }
  138. //Declare valid OSC messages here
  139. if (!OSCin.hasError()) {
  140. OSCin.dispatch("/test", printTest);
  141. // //STEPPER CONTROL
  142. // OSCin.dispatch("/stepper/init", init_Stepper_OSC);
  143. OSCin.dispatch("/stepper/getStatus", getStatus_stepper_OSC);
  144. OSCin.dispatch("/stepper/setCurrentPosition", setCurrentPosition_Stepper_OSC);
  145. OSCin.dispatch("/stepper/currentPosition", currentPos_Stepper_OSC);
  146. OSCin.dispatch("/stepper/driveMode", drivingMode_Stepper_OSC);
  147. OSCin.dispatch("/stepper/moveTo", moveTo_Stepper_OSC);
  148. OSCin.dispatch("/stepper/move", move_Stepper_OSC);
  149. // OSCin.dispatch("/stepper/moveTo/cm", moveToCm_Stepper_OSC);
  150. // OSCin.dispatch("/stepper/move/cm", moveCm_Stepper_OSC);
  151. OSCin.dispatch("/stepper/setSpeed", setSpeed_Stepper);
  152. OSCin.dispatch("/stepper/maxSpeed", maxSpeed_Stepper);
  153. OSCin.dispatch("/stepper/acceleration", acceleration_Stepper);
  154. // OSCin.dispatch("/stepper/randomMove", randomMove_Stepper_OSC);
  155. //
  156. // //LEDS
  157. // OSCin.dispatch("/led/value", valueLed_OSC);
  158. // OSCin.dispatch("/led/sensor", sensorLed_OSC);
  159. // OSCin.dispatch("/led/lighting", lightingLed_OSC);
  160. }
  161. // }
  162. }