main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <Arduino.h>
  2. #include <Automaton.h>
  3. //#include "Atm_lien/Atm_stepper.h"
  4. #include "Atm_Tstepper.h"
  5. #include "Atm_TeensyStep.h"
  6. #include <SPI.h>
  7. #include <Ethernet.h>
  8. #include <EthernetUdp.h>
  9. #include <TeensyMAC.h>
  10. #include <OSCMessage.h>
  11. //////////////// Ethernet /////////////////////////////
  12. // Enter a MAC address and IP address for your controller below.
  13. // The IP address will be dependent on your local network:
  14. byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
  15. IPAddress ip(192, 168, 1, 204); //local IP of Arduino/Teensy
  16. //unsigned int localPort = 8888; // local port to listen on (not needed for multicast)
  17. IPAddress ipMulti(239, 200, 200, 200); // ipMidi Multicast address
  18. unsigned int portMulti = 9977; // ipMidi Mutlicast port 1
  19. // buffers for receiving and sending data
  20. byte packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet
  21. byte sendBuffer1[] = {0x90, 0x14, 0x22}; // MIDI Note On to Multicast address
  22. byte sendBuffer2[] = {0x80, 0x14, 0x00}; // MIDI Note Off to Multicast address
  23. // An EthernetUDP instance to let us send and receive packets over UDP
  24. EthernetUDP Udp;
  25. /////////////////// OSC ////////////////////////
  26. OSCBundle bndl;
  27. /////////////////// STEPPER machines ////////////////////////
  28. /*move are always meant in full steps on the motor output (after reduction)
  29. so all motors have 200steps per turn */
  30. const int BARREL_SPEED = 8000 ;
  31. const int BARREL_ACC = 800 ;
  32. const int BARREL_DISTANCE = 10 ; //in full steps
  33. const int BARREL_REDUCTION_RATIO = 26.85;
  34. const int BARREL_MICROSTEP = 32;
  35. static uint16_t BARREL_THRESHOLD[] = {5} ; //for sensor to trig
  36. const int BARREL_INIBSENSOR = 5000;
  37. const int PILL_SPEED = 10 ;
  38. const int PILL_ACC = 1000 ;
  39. const int PILL_DISTANCE = 200 ; //in full steps
  40. const int PILL_MICROSTEP = 32;
  41. Atm_TeensyStep barrel_step;
  42. Stepper barrel_stepper(3, 2);
  43. StepControl barrel_controller ;
  44. Atm_comparator barrel_sensor;
  45. Atm_TeensyStep pill_step;
  46. Stepper pill_stepper(6, 5);
  47. StepControl pill_controller ;
  48. Atm_comparator pill_sensor ;
  49. void barrel_homing(){
  50. barrel_step.move(BARREL_DISTANCE*BARREL_MICROSTEP*BARREL_REDUCTION_RATIO);
  51. automaton.delay(BARREL_INIBSENSOR); //let the current position go
  52. bool foundHome = false ;
  53. while(barrel_step.state() != barrel_step.STOPPING){
  54. // automaton.run();
  55. barrel_step.cycle();
  56. if(barrel_sensor.state()<BARREL_THRESHOLD[0]){
  57. barrel_step.emergencyStop();
  58. barrel_stepper.setPosition(0);
  59. foundHome = true ;
  60. break ;
  61. }
  62. }
  63. digitalWrite( 13, LOW );
  64. if (!foundHome){Serial.println("homing 0");}
  65. else{Serial.println("homing 1");}
  66. }
  67. void barrel_move(int stepRel){
  68. Serial.println(stepRel);
  69. barrel_step.move(stepRel);
  70. }
  71. void pill_move(int stepRel){
  72. Serial.println(stepRel);
  73. pill_step.move(stepRel);
  74. }
  75. void pill_next(){
  76. pill_move(200 * PILL_MICROSTEP);
  77. //make one turn, if nothing was seen by sensor try again up to 5 times
  78. bool foundPill = false ;
  79. while(pill_step.state() != barrel_step.STOPPING){
  80. automaton.run();
  81. if(pill_sensor.state()<BARREL_THRESHOLD[0]){foundPill = true ;}
  82. }
  83. if (!foundPill){Serial.println("pill 0");}
  84. else{Serial.println("pill 1");}
  85. }
  86. ////////////// Serial command machine /////////////////////
  87. char cmd_buffer[80];
  88. Atm_command cmd;
  89. char cmd1_buffer[80];
  90. Atm_command cmd1;
  91. enum { CMD_TEST, CMD_BARREL_MOVE, CMD_BARREL_HOME, CMD_BARREL_NEXT,
  92. CMD_PILL, CMD_PILL_MOVE };
  93. const char cmdlist[] =
  94. "test barrel_move barrel_home barrel_next pill pill_move";
  95. void cmd_callback( int idx, int v, int up ) {
  96. //int pin = atoi( cmd.arg( 1 ) );
  97. Serial.print(v);
  98. Serial.println(" in callback");
  99. switch ( v ) {
  100. case CMD_TEST:
  101. digitalWrite( 13, HIGH );
  102. Serial.println("in test");
  103. return;
  104. case CMD_BARREL_MOVE:
  105. barrel_move(atoi( cmd.arg( 1 ) ));
  106. return;
  107. case CMD_BARREL_HOME:
  108. // barrel_step.homing(0);
  109. barrel_homing();
  110. return;
  111. case CMD_BARREL_NEXT:
  112. barrel_move(BARREL_DISTANCE);
  113. if((!barrel_sensor.state())<BARREL_THRESHOLD[0]){barrel_homing();}
  114. return;
  115. case CMD_PILL:
  116. pill_next();
  117. return;
  118. case CMD_PILL_MOVE:
  119. pill_move(atoi( cmd.arg( 1 ) ));
  120. return;
  121. }
  122. }
  123. ////////////// Setup /////////////////////
  124. void setup() {
  125. //Configure and start ethernet module (not needed for feeder)
  126. // SPI.setSCK(27);
  127. // Ethernet.init(15);//(10)
  128. // teensyMAC(mac);
  129. // Ethernet.begin(mac, ip); // start the Ethernet and UDP:
  130. // // Udp.beginMulti(ipMulti, portMulti); // for modified Arduino library
  131. // Udp.beginMulticast(ipMulti, portMulti); // for modified Teensy Ethernet library
  132. //Start serial
  133. Serial.begin(115200); // higher Baud rate for faster refresh, CHANGE IN SERIAL MONITOR
  134. Serial1.begin(9600);
  135. delay(2000);
  136. Serial.println("Started");
  137. cmd.begin( Serial, cmd_buffer, sizeof( cmd_buffer ) )
  138. .trace(Serial)
  139. .list( cmdlist )
  140. .onCommand( cmd_callback );
  141. cmd1.begin( Serial1, cmd1_buffer, sizeof( cmd1_buffer ) )
  142. .trace(Serial)
  143. .list( cmdlist )
  144. .onCommand( cmd_callback );
  145. pinMode(13, OUTPUT);
  146. // pinMode(4, OUTPUT);
  147. // pinMode(4, LOW);
  148. barrel_step.trace( Serial );
  149. barrel_step.begin(barrel_stepper, barrel_controller, Udp, bndl, "/Y_top")
  150. .setEnablePin(4).enableReversed(1);
  151. //.setLimitType(3).setLimitPins(A3).limitThresholds(800, 1000, 0, 100).homing(1);
  152. barrel_stepper.setMaxSpeed(BARREL_SPEED);
  153. barrel_stepper.setAcceleration(BARREL_ACC);
  154. barrel_stepper.setInverseRotation(true);
  155. barrel_sensor.begin(A3, 50).threshold(BARREL_THRESHOLD, 1)
  156. .onChange( []( int idx, int v, int up ){Serial.println(up);} )
  157. .onChange( true, []( int idx, int v, int up ){Serial.println(up);} )
  158. .onChange( false, []( int idx, int v, int up ){Serial.println(up);} );
  159. pill_step.trace( Serial );
  160. pill_step.begin(pill_stepper, pill_controller, Udp, bndl, "/Y_top")
  161. .setEnablePin(7).enableReversed(1);
  162. pill_stepper.setMaxSpeed(PILL_SPEED);
  163. pill_stepper.setAcceleration(PILL_ACC);
  164. pill_sensor.begin(A1, 50);
  165. //stepper.onOnchange(Machine &machine, optional int event = 0)
  166. //stepper.cycle(1000);
  167. //barrel_step.move(10000);
  168. // pill_step.move(10000);
  169. //controller.moveAsync(*stepper.motor);
  170. }
  171. void loop() {
  172. automaton.run();
  173. //Serial.println(barrel_sensor.state());
  174. }