浏览代码

removed stepper_update from move() command and updated example

eLandon 4 年之前
父节点
当前提交
fc76f7e6a2
共有 2 个文件被更改,包括 88 次插入21 次删除
  1. 1 1
      Atm_AccelStepper.cpp
  2. 87 20
      examples/Atm_AccelStepper/Atm_AccelStepper.ino

+ 1 - 1
Atm_AccelStepper.cpp

@@ -518,7 +518,7 @@ Atm_AccelStepper& Atm_AccelStepper::rotate( long  int speed) {
   runMode = 1;
   stepper->setSpeed( speed);
   _currentSpeed = stepper->speed();
-  stepper_update();
+  // stepper_update();
   enable();
   trigger( EVT_MOVE );
   return *this;

+ 87 - 20
examples/Atm_AccelStepper/Atm_AccelStepper.ino

@@ -1,21 +1,88 @@
-#include <Automaton.h>
-#include "Atm_AccelStepper.h"
-
-// Basic Arduino sketch - instantiates the state machine and nothing else
-
-Atm_AccelStepper AccelStepper;
-
-void setup() {
-
-  // Serial.begin( 9600 );
-  // AccelStepper.trace( Serial );
-
-  AccelStepper.begin();
-
-}
-
-void loop() {
-  automaton.run();
-}
-
+#include <Automaton.h>
+#include "Atm_AccelStepper.h"
 
+
+Atm_AccelStepper stepper ;
+int STEPPER_STEPPIN = 25 ;
+int STEPPER_DIRPIN = 26 ;
+int STEPPER_MAXSPEED = 1000000 ;
+int STEPPER_ACCELERATION = 100000;
+
+Atm_timer timer;
+char cmd_buffer[80];
+Atm_command cmd;
+
+enum { CMD_M, CMD_M2, CMD_R, CMD_S, CMD_P };
+const char cmdlist[] = 
+  "m m2 r s p";
+
+void cmd_callback( int idx, int v, int up ) {
+  int pin = atoi( cmd.arg( 1 ) );
+  switch ( v ) {
+    case CMD_M: 
+      Serial.print("move : ");
+      Serial.println(pin);
+      stepper.move(pin);
+      return;
+    case CMD_M2:
+      Serial.print("moveTo : ");
+      Serial.println(pin);
+      stepper.moveTo(pin);
+      return;
+    case CMD_R: 
+      Serial.print("rotate : ");
+      Serial.println(pin);
+      stepper.rotate(pin);
+      return;
+    case CMD_S:
+      Serial.println("stop");
+      stepper.stop();
+      return;
+      case CMD_P:
+      Serial.print("position : ");
+      Serial.println(stepper.getPosition());
+      return;
+      }
+}
+
+void timer_callback( int idx, int v, int up ) {
+  //Serial.println("timer");
+  Serial.print("position : ");
+  Serial.println(stepper.getPosition());
+  stepper.rotate(50);
+}
+
+void setup() {
+
+  Serial.begin( 9600 );
+  cmd.begin( Serial, cmd_buffer, sizeof( cmd_buffer ) )
+    .list( cmdlist )
+    .onCommand( cmd_callback );
+
+    
+  // AccelStepper.trace( Serial );
+
+  stepper.begin(STEPPER_STEPPIN, STEPPER_DIRPIN)
+         .pinReversed(0, 0, 0)
+         .setMaxSpeed(STEPPER_MAXSPEED)
+         .setAcceleration(STEPPER_ACCELERATION)
+         .enable();
+  stepper.trace(Serial);
+
+  stepper.limitLow_set(1, 15, 0).limitLow_isHard(1)//.limitLow_setThresholds(1200, 2300)
+         .limitHigh_set(1, 2, 0).limitHigh_isHard(1)      
+          // .onOnlimitlow([](int idx, int v, int up){ stepper.setPosition(0);stepper.move(1000);})
+         .onOnlimitlow([](int idx, int v, int up){ Serial.println('limit low');})
+          //   // .onOnlimithigh([](int idx, int v, int up){stepper.setPosition(DAY_STEPS*MICROSTEPPING);})
+         .onOnlimithigh([](int idx, int v, int up){Serial.println('limit high');})
+            ;
+
+   timer.begin(2000).repeat(-1).onTimer(timer_callback).start();
+   
+
+}
+
+void loop() {
+  automaton.run();
+  //Serial.println(stepper.limitLow_State);
+}