78#ifndef HORNET_STEPPER_H
79#define HORNET_STEPPER_H
81#include <AccelStepper.h>
93 unsigned int position;
102 const int mechZero = 0;
106 unsigned int capValue;
111 uint8_t numMapPoints;
112 unsigned int* inputVals;
113 unsigned int* outputPos;
116 AccelStepper stepper;
117 bool homingInProgress;
123 static bool ufcEntPressed;
124 static bool ufcClrPressed;
145 int zeroPos,
int maxPos,
int dirForward,
146 unsigned int capValue = 65535,
147 int normalSpeed = 300,
148 int normalAccel = 600,
150 uint8_t numMapPoints = 0)
151 : stepper(AccelStepper::FULL4WIRE,
152 (dirForward == -1) ? coil3 : coil1,
153 (dirForward == -1) ? coil4 : coil2,
154 (dirForward == -1) ? coil1 : coil3,
155 (dirForward == -1) ? coil2 : coil4)
157 this->zeroPos = zeroPos;
158 this->maxPos = maxPos;
159 this->dirForward = dirForward;
160 this->capValue = capValue;
161 this->normalSpeed = normalSpeed;
162 this->normalAccel = normalAccel;
163 this->homingInProgress =
false;
164 this->testInProgress =
false;
166 stepper.setMaxSpeed(normalSpeed);
167 stepper.setAcceleration(normalAccel);
170 if (mapPoints !=
nullptr && numMapPoints > 0) {
172 this->mapPoints = mapPoints;
173 this->numMapPoints = numMapPoints;
174 this->useMultiMap =
true;
177 this->inputVals =
new unsigned int[numMapPoints];
178 this->outputPos =
new unsigned int[numMapPoints];
179 for (uint8_t i = 0; i < numMapPoints; i++) {
180 this->inputVals[i] = mapPoints[i].value;
181 this->outputPos[i] = mapPoints[i].position;
185 this->mapPoints =
nullptr;
186 this->inputVals =
nullptr;
187 this->outputPos =
nullptr;
188 this->numMapPoints = 0;
189 this->useMultiMap =
false;
197 if (useMultiMap && inputVals !=
nullptr) {
213 int zeroingSpeed = 20;
214 int zeroingAccel = 10;
215 stepper.setMaxSpeed(zeroingSpeed);
216 stepper.setAcceleration(zeroingAccel);
218 stepper.setCurrentPosition(maxPos);
219 stepper.runToNewPosition(mechZero);
220 stepper.setCurrentPosition(mechZero);
221 stepper.runToNewPosition(zeroPos);
223 stepper.setMaxSpeed(normalSpeed);
224 stepper.setAcceleration(normalAccel);
234 testInProgress =
true;
235 stepper.setMaxSpeed(testSpeed);
236 stepper.setAcceleration(testAccel);
238 stepper.runToNewPosition(maxPos);
240 stepper.runToNewPosition(zeroPos);
242 stepper.setMaxSpeed(normalSpeed);
243 stepper.setAcceleration(normalAccel);
244 testInProgress =
false;
259 unsigned int trimmedVal = min(targetVal, capValue);
265 targetPos = multiMapCache<unsigned int>(trimmedVal, inputVals, outputPos, numMapPoints);
267 targetPos = map(trimmedVal, 0, capValue, zeroPos, maxPos);
271 stepper.moveTo(targetPos);
281 bool shouldHome = ufcEntPressed && ufcClrPressed;
283 if (shouldHome && !homingInProgress && !testInProgress) {
284 homingInProgress =
true;
287 homingInProgress =
false;
296bool HornetStepper::ufcEntPressed =
false;
297bool HornetStepper::ufcClrPressed =
false;
300void onUfcEntChange(
unsigned int newVal) {
302 HornetStepper::ufcEntPressed = (newVal != 0);
304DcsBios::IntegerBuffer ufcEntBuffer(FA_18C_hornet_UFC_ENT, onUfcEntChange);
306void onUfcClrChange(
unsigned int newVal) {
308 HornetStepper::ufcClrPressed = (newVal != 0);
310DcsBios::IntegerBuffer ufcClrBuffer(FA_18C_hornet_UFC_CLR, onUfcClrChange);
Common class to control stepper-run gauges in OpenHornet.
~HornetStepper()
Destructor to clean up allocated arrays.
void findZero()
findZero() is a function to zero the gauge
void setTarget(unsigned int targetVal)
setTarget() is a function to set the target position of the gauge
HornetStepper(int coil1, int coil2, int coil3, int coil4, int zeroPos, int maxPos, int dirForward, unsigned int capValue=65535, int normalSpeed=300, int normalAccel=600, MapPoint *mapPoints=nullptr, uint8_t numMapPoints=0)
Constructor for HornetStepper.
void run()
run() is a function to run the stepper motor (non-blocking)
void testFullRange(int testSpeed=20, int testAccel=10)
testFullRange() is a function to test the range of the gauge
Only in case of non-linear gauges, this struct is used. It represents a value-position mapping pair.