AccelStepperI2C  v 0.1
I2C wrapper (and a bit more) for the AccelStepper Arduino library
Speed_diagnostics.ino
Go to the documentation of this file.
1 /*
2  AccelStepperI2C speed tests
3  (c) juh 2022
4 
5  Generate the data for the performance graphs included in the documentation.
6  Copy and paste the output to a spreadsheet for further analysis.
7 
8  1. install library to Arduino environment
9  2. in firmware.ino, enable (uncomment) DIAGNOSTICS_AccelStepperI2C and disable (comment)
10  DEBUG_AccelStepperI2C. Upload firmware.ino to testing platform.
11  4. upload this example to another Arduino-like (master), I used a Wemos D1 mini with ESP8266
12  4. connect the I2C bus of both devices (usually A4<>A4, A5<>A5, GND<>GND)
13  5. don't fortget two I2C pullups and level-shifters if needed
14  6. also connect +5V<>+5V to power one board from the other, if needed
15  7. Don't connect drivers and steppers for these speed tests.
16 */
17 
18 #include <Arduino.h>
19 #include <AccelStepperI2C.h>
20 #include <Wire.h>
21 
22 //#define DEBUG_AccelStepperI2C
23 
24 const uint8_t addr = 0x8; // i2c address of slave
25 const uint32_t msp = 30000000;
26 
27 const byte maxSteppers = 8;
29 byte numSteppers = 0;
30 
34 
35 void setup()
36 {
37 
38  // Important: initialize Wire before creating AccelStepperI2C objects
39  Wire.begin();
40  Serial.begin(115200);
41  while (!Serial) {
42  }
43 
44  Serial.println("\n\n\nAccelStepperI2C demo - Speed tests\n\n");
45 
46  // If the slave's and master's reset is not synchronized by hardware, after a master's reset the slave might
47  // think the master wants another stepper, not a first one, and will run out of steppers, sooner or later.
48  Serial.print("\n\nresetting slave\n");
49  resetAccelStepperSlave(addr);
50  delay(500);
51 
55 
56  Serial.println("\n\nMeasuring processing times (in milliseconds)\n\n");
57 
58  for (byte i = 0; i < maxSteppers; i++) {
59  S[i] = new AccelStepperI2C(addr, AccelStepper::DRIVER, /* step pin */ 4, /* dir pin */ 5);
60  //Serial.print("\nStepper added with no. "); Serial.println(S[i]->myNum);
61  Serial.print(S[i]->myNum + 1); Serial.println(" steppers attached");
62 
63  numSteppers++;
64  if (i == 0) {
65  S[0]->enableDiagnostics(); // call only once
66  }
67  testThem(100);
68  }
69 
70 }
71 
72 uint32_t countCycles(int millis)
73 {
74  S[0]->diagnostics(report0); // make cycles reset, so we don't risk an overrun
75  S[0]->diagnostics(report0);
76  // uint32_t then = millis() + millis;
77  // while (millis() < then) {
78  // if (!S[i].isRunning()) {
79  // S[i].
80  // }
81  // }
82  delay(millis);
83  S[0]->diagnostics(report1);
84  return ((report1->cycles) - (report0->cycles)) * (1000 / millis);
85 }
86 
87 void resetStepper(uint8_t stp)
88 {
89  S[stp]->stopState();
90  S[stp]->setSpeed(0);
91  S[stp]->setCurrentPosition(0);
92  S[stp]->moveTo(0);
93  S[stp]->setAcceleration(0);
94 }
95 
97 {
98  for (byte i = 0; i < numSteppers; i++) {
99  resetStepper(i);
100  }
101  delay(20);
102 }
103 
104 void testThem(uint32_t ms)
105 {
106  resetSteppers();
107 
108  Serial.print("runSpeed: ");
109  for (byte i = 0; i < maxSteppers; i++) {
110  if (i < numSteppers) {
111  S[i]->setMaxSpeed(msp * 2); S[i]->setSpeed(msp);
112  S[i]->runSpeedState();
113  Serial.print(countCycles(ms)); Serial.print((S[i]->getState() != 0) ? " " : "_");
114  } else {
115  Serial.print(0); Serial.print(" ");
116  }
117  }
118  Serial.println();
119  resetSteppers();
120 
121  Serial.print("runSpeedToPosition: ");
122  for (byte i = 0; i < maxSteppers; i++) {
123  if (i < numSteppers) {
124  S[i]->setMaxSpeed(msp * 2); S[i]->setSpeed(msp);
125  S[i]->moveTo(0x7fffffff);
127  Serial.print(countCycles(ms)); Serial.print((S[i]->getState() != 0) ? " " : "_");
128  } else {
129  Serial.print(0); Serial.print(" ");
130  }
131  }
132  Serial.println();
133  resetSteppers();
134 
135  Serial.print("run: ");
136  for (byte i = 0; i < maxSteppers; i++) {
137  if (i < numSteppers) {
138  S[i]->setAcceleration(10000000.0); S[i]->moveTo(0x7fffffff);
139  S[i]->setMaxSpeed(msp * 2); S[i]->runState();
140  Serial.print(countCycles(ms)); Serial.print((S[i]->getState() != 0) ? " " : "_");
141  Serial.print("["); Serial.print(S[i]->distanceToGo()); Serial.print("] ");
142  } else {
143  Serial.print(0); Serial.print(" ");
144  }
145  }
146  Serial.println();
147  resetSteppers();
148 
149 }
150 
151 // use this to get an idea how long the slave spends in onRequest() and onReceive() interrupts
152 // and how long it takes to process a command from the master
154 {
155  S[0]->diagnostics(report);
156  Serial.print("lastProcessTime = "); Serial.println(report->lastProcessTime);
157  Serial.print("lastRequestTime = "); Serial.println(report->lastRequestTime);
158  Serial.print("lastReceiveTime = "); Serial.println(report->lastReceiveTime);
159 }
160 
161 void loop()
162 {
163 
164 }
report0
diagnosticsReport * report0
Definition: Speed_diagnostics.ino:32
AccelStepperI2C::moveTo
void moveTo(long absolute)
Definition: AccelStepperI2C.cpp:49
report1
diagnosticsReport * report1
Definition: Speed_diagnostics.ino:33
AccelStepperI2C::diagnostics
void diagnostics(diagnosticsReport *report)
Get most recent diagnostics data. Needs diagnostics enabled and a slave which was compiled with the D...
Definition: AccelStepperI2C.cpp:285
AccelStepperI2C::stopState
void stopState()
Will stop any of the above states, i.e. stop polling. It does nothing else, so the master is solely i...
Definition: AccelStepperI2C.cpp:314
AccelStepperI2C::runSpeedState
void runSpeedState()
Will poll runSpeed(), i.e. run at constant speed until told otherwise (see AccelStepperI2C::stopState...
Definition: AccelStepperI2C.cpp:326
diagnosticsReport::cycles
uint32_t cycles
Number of slave's main loop executions since the last reboot.
Definition: AccelStepperI2C.h:87
countCycles
uint32_t countCycles(int millis)
Definition: Speed_diagnostics.ino:72
report
diagnosticsReport * report
Definition: Speed_diagnostics.ino:31
AccelStepperI2C
An I2C wrapper class for the AccelStepper library.
Definition: AccelStepperI2C.h:163
AccelStepperI2C.h
AccelStepperI2C::setMaxSpeed
void setMaxSpeed(float speed)
Definition: AccelStepperI2C.cpp:142
addr
const uint8_t addr
Definition: Speed_diagnostics.ino:24
diagnosticsReport::lastProcessTime
uint16_t lastProcessTime
microseconds the slave needed to process (interpret) most recently received command
Definition: AccelStepperI2C.h:88
AccelStepperI2C::enableDiagnostics
void enableDiagnostics(bool enable)
Turn on/off diagnostic speed logging.
Definition: AccelStepperI2C.cpp:277
resetSteppers
void resetSteppers()
Definition: Speed_diagnostics.ino:96
AccelStepperI2C::runSpeedToPositionState
void runSpeedToPositionState()
Will poll runSpeedToPosition(), i.e. run at constant speed until target has been reached.
Definition: AccelStepperI2C.cpp:332
AccelStepperI2C::runState
void runState()
Will poll run(), i.e. run to the target with acceleration and stop the state machine upon reaching it...
Definition: AccelStepperI2C.cpp:320
AccelStepperI2C::setAcceleration
void setAcceleration(float acceleration)
Definition: AccelStepperI2C.cpp:161
setup
void setup()
Definition: Speed_diagnostics.ino:35
testThem
void testThem(uint32_t ms)
Definition: Speed_diagnostics.ino:104
diagnosticsReport
Used to transmit diagnostic info with AccelStepperI2C::diagnostics().
Definition: AccelStepperI2C.h:85
maxSteppers
const byte maxSteppers
Definition: Speed_diagnostics.ino:27
AccelStepperI2C::setSpeed
void setSpeed(float speed)
Definition: AccelStepperI2C.cpp:169
msp
const uint32_t msp
Definition: Speed_diagnostics.ino:25
numSteppers
byte numSteppers
Definition: Speed_diagnostics.ino:29
loop
void loop()
Definition: Speed_diagnostics.ino:161
AccelStepperI2C::setCurrentPosition
void setCurrentPosition(long position)
Definition: AccelStepperI2C.cpp:134
S
AccelStepperI2C * S[maxSteppers]
Definition: Speed_diagnostics.ino:28
processingTimes
void processingTimes()
Definition: Speed_diagnostics.ino:153
diagnosticsReport::lastRequestTime
uint16_t lastRequestTime
microseconds the slave spent in the most recent onRequest() interrupt
Definition: AccelStepperI2C.h:89
resetStepper
void resetStepper(uint8_t stp)
Definition: Speed_diagnostics.ino:87
diagnosticsReport::lastReceiveTime
uint16_t lastReceiveTime
microseconds the slave spent in the most recent onReceive() interrupt
Definition: AccelStepperI2C.h:90