فهرست منبع

First commit

raw copying of OSC and stepper parts from M. Lavenne project
Etienne Landon 7 سال پیش
کامیت
847d69f483
3فایلهای تغییر یافته به همراه243 افزوده شده و 0 حذف شده
  1. 18 0
      C_Vieille_OSCstepperCtl.ino
  2. 111 0
      osc.h
  3. 114 0
      stepper.h

+ 18 - 0
C_Vieille_OSCstepperCtl.ino

@@ -0,0 +1,18 @@
+#include "stepper.h"
+#include "osc.h"
+
+
+
+
+void setup() {
+  setup_OSC();
+  setup_Stepper();
+  delay(1000);
+  init_Stepper() 
+
+}
+
+void loop() {
+  handleOSCIn();
+  update_Stepper();
+}

+ 111 - 0
osc.h

@@ -0,0 +1,111 @@
+#include <OSCMessage.h>
+#include <Ethernet.h>
+#include <EthernetUdp.h>
+#include <SPI.h>
+
+
+
+template <typename TYPE> void sendOSC(const char * adress, TYPE parameter) {
+  OSCMessage OSCmsg(adress);
+  OSCmsg.add(parameter);
+  Udp.beginPacket(outIp, outPort);
+  OSCmsg.send(Udp); // send the bytes to the SLIP stream
+  Udp.endPacket(); // mark the end of the OSC Packet
+  OSCmsg.empty(); // free space occupied by message
+}
+
+/////  STEPPER /////////
+
+void init_Stepper_OSC(OSCMessage &msg) {
+  float maximum = init_Stepper();
+  sendOSC("/stepper/maxPos",maximum );
+}
+
+void moveTo_Stepper_OSC (OSCMessage &msg){
+  long int distance = moveTo_Stepper(msg.getInt(0));
+  sendOSC("/stepper/distanceToGo",distance );
+}
+
+void move_Stepper_OSC (OSCMessage &msg){
+  long int distance = move_Stepper(msg.getInt(0));
+  sendOSC("/stepper/distanceToGo",distance );
+}
+
+void moveToCm_Stepper_OSC (OSCMessage &msg){
+  long int distance = moveToCm_Stepper(msg.getFloat(0));
+  sendOSC("/stepper/distanceToGo/cm",distance );
+}
+
+void moveCm_Stepper_OSC (OSCMessage &msg){
+  long int distance = moveCm_Stepper(msg.getFloat(0));
+  sendOSC("/stepper/distanceToGo/cm",distance );
+}
+
+void maxSpeed_Stepper (OSCMessage &msg){
+  long int max_speed = maxSpeed_Stepper(msg.getInt(0));
+  sendOSC("/stepper/maxSpeed",max_speed );
+}
+
+void acceleration_Stepper (OSCMessage &msg){
+  long int acceleration = acceleration_Stepper(msg.getInt(0));
+  sendOSC("/stepper/acceleration",acceleration );
+
+
+//////  OSC DECLARATIONS  /////////
+
+void setup_OSC(){
+  OSCMessage OSCmsg("/connected");
+
+
+}
+
+
+
+void handleOSCIn() {
+
+//  //check ethernet connection periodically
+//  if ( millis() - ethernetTestTimer > 5000 ) {
+//    ethernetTestTimer = millis() ;  
+//    //Serial.println(Ethernet.maintain());    
+//  }
+  
+  //define OSC message to function relationships
+  OSCMessage OSCin;
+  int size;
+
+  if ( (size = Udp.parsePacket()) > 0) {
+    while (size--)
+    OSCin.fill(Udp.read());
+
+    //Declare valid OSC messages here 
+    if (!OSCin.hasError()) {
+      OSCin.dispatch("/test", printTest);
+      OSCin.dispatch("/mode", mode_OSC);
+      
+      //SENSOR
+      OSCin.dispatch("/sensor/state", sensorState_OSC);
+      OSCin.dispatch("/sensor/time", sensorTimeOSC);
+      OSCin.dispatch("/sensor/scale/reset", resetScale_OSC);
+      OSCin.dispatch("/sensor/scale/current", currentScale_OSC);
+
+      //CC MOTOR CONTROL
+      OSCin.dispatch("/CC/speed", setSpeed_CCMotor_OSC);
+      OSCin.dispatch("/CC/cs", checkCS_CCMotor_OSC);
+      
+      //STEPPER CONTROL
+      OSCin.dispatch("/stepper/init", init_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);
+    }    
+  }
+}

+ 114 - 0
stepper.h

@@ -0,0 +1,114 @@
+#include <AccelStepper.h>
+/*
+ * stepper control through big easy driver
+ * positions are expressed in centimeters
+ * originPos is near the motor, where switchLow gets LOW
+ * maxPos is the maximum position, where switchHigh gets LOW
+ * idlePos is the idle position
+ * lowOffset 
+ * highOffset are offsets from originPos/maxPos respectively to define the running zone
+ * 
+ * Stepper driven through big easy driver
+ * configured for 1/4 microstepping (mover micro-steps gives low speed)
+ * 800 steps is one rotation
+ * one rotation is 1.6cm translation
+ * cm to steps ratio is 500 
+ */
+
+ // Define a stepper and the pins it will use
+AccelStepper stepper(1, 8, 7);
+
+// microswitch pins
+int switchLow = A5;
+
+int speedLimit[2] = { 50 , 1000 } ;// sets an absolute minimum/maximum speed limit
+int accLimit[2] = { 100, 2000} ;
+
+long int moveTo_Stepper(long int stepto ) {
+  stepper.moveTo( constrain( stepto , originPos * stepToCm, maxPos * stepToCm ) );
+  return stepper.distanceToGo();
+}
+float moveToCm_Stepper(float cmto ) {
+  stepper.moveTo(constrain( cmto * stepToCm , originPos, maxPos ));
+  return stepper.distanceToGo()/stepToCm;
+}
+
+long int move_Stepper(long int stepRelative ) {
+  stepper.move(stepRelative);
+  return stepper.distanceToGo();
+}
+float moveCm_Stepper(float cmRelative ) {
+  stepper.move(cmRelative * stepToCm );
+  return stepper.distanceToGo()/stepToCm;
+}
+
+long int maxSpeed_Stepper(long int maxspeed){
+  stepper.setMaxSpeed( constrain (maxspeed, speedLimit[0], speedLimit[1] ) );
+  return stepper.maxSpeed();
+}
+
+long int acceleration_Stepper( long int acc ){
+  stepper.setAcceleration(acc + 1); // prevent 0 step/s²
+  return acc;
+}
+
+//////////////// CALIBRATION ////////////////////////
+
+float init_Stepper(){
+  //run at constant speed until hitting switchLow
+  stepper.setSpeed(-1*speedLimit[1]); 
+  while(digitalRead(switchLow)) {
+    stepper.runSpeed();
+  }
+  stepper.stop();
+  //this is the new 0 step
+  stepper.setCurrentPosition(0);
+
+
+
+  delay(1000); //take a breath
+
+  //run at constant speed until hitting switchHigh
+  stepper.setSpeed(speedLimit[1]);
+  while(digitalRead(switchHigh)) {
+    stepper.runSpeed(); 
+  }
+  stepper.stop();
+  maxPos = stepper.currentPosition() / stepToCm ;
+
+  delay(1000);
+
+  stepper.runToNewPosition(maxPos * stepToCm / 2) ; 
+  return maxPos ;
+}
+
+///////////////////// SETUP ///////////////////////////
+void setup_Stepper(){
+  pinMode (switchLow, INPUT_PULLUP);
+  pinMode (switchHigh, INPUT_PULLUP);
+  stepper.setMaxSpeed(speedLimit[1]);
+  //stepper.setSpeed(10000);
+  stepper.setAcceleration(accLimit[1]);
+}
+
+///////////////////// LOOP ///////////////////////////
+
+bool update_Stepper() {
+  
+  bool updated = 1 ;
+  stepper.run();
+  
+  if (!digitalRead(switchLow)) {
+    stepper.setCurrentPosition(0);
+    updated=0;
+  }
+  if (!digitalRead(switchHigh)) {
+    stepper.setCurrentPosition(maxPos * stepToCm);
+    updated=0;
+  }
+  return updated ;
+}
+
+
+
+