osc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. //// UTILITIES /////////
  15. template <typename TYPE> void sendOSC(const char * adress, int first_param, TYPE parameter) {
  16. OSCMessage OSCmsg(adress);
  17. OSCmsg.add(first_param);
  18. OSCmsg.add(parameter);
  19. SLIPSerial.beginPacket();
  20. //Udp.beginPacket(outIp, outPort);
  21. OSCmsg.send(SLIPSerial); // send the bytes to the SLIP stream
  22. // OSCmsg.send(Udp); // send the bytes to the SLIP stream
  23. SLIPSerial.endPacket();
  24. //Udp.endPacket(); // mark the end of the OSC Packet
  25. OSCmsg.empty(); // free space occupied by message
  26. }
  27. void printTest(OSCMessage &msg) {
  28. sendOSC("/still", 3, "up and running");
  29. }
  30. ///// STEPPER /////////
  31. //void init_Stepper_OSC(OSCMessage &msg) {
  32. // float maximum = init_Stepper();
  33. // sendOSC("/stepper/maxPos",maximum );
  34. //}
  35. //
  36. void setCurrentPosition_Stepper_OSC (OSCMessage &msg) {
  37. int index = msg.getInt(0);
  38. steppers[index].setCurrentPosition(msg.getInt(1));
  39. sendOSC("/stepper/setPosition/", index, "position set");
  40. }
  41. void drivingMode_Stepper_OSC (OSCMessage &msg) {
  42. driveMode = msg.getInt(0);
  43. sendOSC("/stepper/driveMode/", 3, "set to " + driveMode);
  44. }
  45. void moveTo_Stepper_OSC (OSCMessage &msg){
  46. int index = msg.getInt(0);
  47. long int distance = moveTo_Stepper(index, msg.getInt(1));
  48. sendOSC("/stepper/distanceToGo", index, distance );
  49. // while (!stepper.distanceToGo()==0)
  50. // stepper.run();
  51. }
  52. void move_Stepper_OSC (OSCMessage &msg){
  53. int index = msg.getInt(0);
  54. long int distance = move_Stepper(index, msg.getInt(1));
  55. sendOSC("/stepper/distanceToGo", index, distance );
  56. }
  57. void maxSpeed_Stepper (OSCMessage &msg){
  58. int index = msg.getInt(0);
  59. long int max_speed = maxSpeed_Stepper(index, msg.getInt(1));
  60. sendOSC("/stepper/maxSpeed", index, max_speed );
  61. }
  62. void acceleration_Stepper (OSCMessage &msg){
  63. int index = msg.getInt(0);
  64. long int acceleration = acceleration_Stepper(index, msg.getInt(1));
  65. sendOSC("/stepper/acceleration", index, acceleration );
  66. }
  67. void currentPos_Stepper_OSC(OSCMessage &msg) {
  68. int index = msg.getInt(0);
  69. sendOSC("/stepper/currentPos", index, steppers[index].currentPosition());
  70. }
  71. void status_Stepper_OSC(int stepper_index) {
  72. OSCMessage OSCmsg("/stepper/status");
  73. OSCmsg.add(stepper_index);
  74. OSCmsg.add(steppers[stepper_index].currentPosition());
  75. OSCmsg.add(steppers[stepper_index].targetPosition());
  76. OSCmsg.add(steppers[stepper_index].speed());
  77. OSCmsg.add(steppers[stepper_index].maxSpeed());
  78. SLIPSerial.beginPacket();
  79. //Udp.beginPacket(outIp, outPort);
  80. OSCmsg.send(SLIPSerial); // send the bytes to the SLIP stream
  81. // OSCmsg.send(Udp); // send the bytes to the SLIP stream
  82. SLIPSerial.endPacket();
  83. //Udp.endPacket(); // mark the end of the OSC Packet
  84. OSCmsg.empty(); // free space occupied by message
  85. }
  86. void getStatus_stepper_OSC(OSCMessage &msg) {
  87. status_Stepper_OSC(msg.getInt(0)) ;
  88. }
  89. ////// OSC DECLARATIONS /////////
  90. void setup_OSC(){
  91. SLIPSerial.begin(115200);
  92. // delay (100);
  93. sendOSC("/ready", 3, "to go");
  94. }
  95. void handleOSCIn() {
  96. //send stepper status while it is running
  97. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  98. if (update_stepper_status[i]) {
  99. status_Stepper_OSC(i) ;
  100. }
  101. if (!steppers[i].isRunning()) { update_stepper_status[i] = 0 ;}
  102. else update_stepper_status[i] = 1 ;
  103. }
  104. //define OSC message to function relationships
  105. OSCMessage OSCin;
  106. int size;
  107. if (SLIPSerial.available()) {
  108. while (!SLIPSerial.endofPacket())
  109. if ( (size = SLIPSerial.available()) > 0)
  110. {
  111. while (size--)
  112. OSCin.fill(SLIPSerial.read());
  113. }
  114. }
  115. //Declare valid OSC messages here
  116. if (!OSCin.hasError()) {
  117. OSCin.dispatch("/test", printTest);
  118. // //STEPPER CONTROL
  119. // OSCin.dispatch("/stepper/init", init_Stepper_OSC);
  120. OSCin.dispatch("/stepper/getStatus", getStatus_stepper_OSC);
  121. OSCin.dispatch("/stepper/setCurrentPosition", setCurrentPosition_Stepper_OSC);
  122. OSCin.dispatch("/stepper/currentPosition", currentPos_Stepper_OSC);
  123. OSCin.dispatch("/stepper/driveMode", drivingMode_Stepper_OSC);
  124. OSCin.dispatch("/stepper/moveTo", moveTo_Stepper_OSC);
  125. OSCin.dispatch("/stepper/move", move_Stepper_OSC);
  126. // OSCin.dispatch("/stepper/moveTo/cm", moveToCm_Stepper_OSC);
  127. // OSCin.dispatch("/stepper/move/cm", moveCm_Stepper_OSC);
  128. OSCin.dispatch("/stepper/maxSpeed", maxSpeed_Stepper);
  129. OSCin.dispatch("/stepper/acceleration", acceleration_Stepper);
  130. // OSCin.dispatch("/stepper/randomMove", randomMove_Stepper_OSC);
  131. //
  132. // //LEDS
  133. // OSCin.dispatch("/led/value", valueLed_OSC);
  134. // OSCin.dispatch("/led/sensor", sensorLed_OSC);
  135. // OSCin.dispatch("/led/lighting", lightingLed_OSC);
  136. }
  137. // }
  138. }