API Documentation v0.0.1
Loading...
Searching...
No Matches
HornetStepper Class Reference

Common class to control stepper-run gauges in OpenHornet. More...

#include <Hornet_Stepper.h>

Public Member Functions

 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.
 
 ~HornetStepper ()
 Destructor to clean up allocated arrays.
 
void findZero ()
 findZero() is a function to zero the gauge
 
void testFullRange (int testSpeed=20, int testAccel=10)
 testFullRange() is a function to test the range of the gauge
 
void setTarget (unsigned int targetVal)
 setTarget() is a function to set the target position of the gauge
 
void run ()
 run() is a function to run the stepper motor (non-blocking)
 

Static Public Attributes

static bool ufcEntPressed = false
 
static bool ufcClrPressed = false
 

Detailed Description

Common class to control stepper-run gauges in OpenHornet.

Uses the AccelStepper library to control the stepper motor.

Definition at line 100 of file Hornet_Stepper.h.

Constructor & Destructor Documentation

◆ HornetStepper()

HornetStepper::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 )
inline

Constructor for HornetStepper.

Parameters
coil1Pin for coil 1
coil2Pin for coil 2
coil3Pin for coil 3
coil4Pin for coil 4
zeroPosPosition of the zero on the dial, in steps from low mechanical stop (absolute position, e.g., 20)
maxPosMaximum number of steps from low mech stop to high mech stop
dirForwardDirection for forward movement (1 or -1)
capValueDCS-BIOS value to cap at (default 65535). This is needed if mech stop is lower than game maximum. Values above this will be auto-capped, preventing the needle from exceeding maxPos position.
normalSpeedNormal operating speed
normalAccelNormal operating acceleration
mapPointsOptional array of MapPoint structs for non-linear mapping (nullptr for linear mapping)
numMapPointsNumber of mapping points (required if mapPoints is provided)

Definition at line 144 of file Hornet_Stepper.h.

151 : stepper(AccelStepper::FULL4WIRE,
152 (dirForward == -1) ? coil3 : coil1,
153 (dirForward == -1) ? coil4 : coil2,
154 (dirForward == -1) ? coil1 : coil3,
155 (dirForward == -1) ? coil2 : coil4)
156 {
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;
165
166 stepper.setMaxSpeed(normalSpeed);
167 stepper.setAcceleration(normalAccel);
168
169 // Handle mapping array - if nullptr passed, use linear mapping
170 if (mapPoints != nullptr && numMapPoints > 0) {
171 // Use provided mapping array for non-linear mapping
172 this->mapPoints = mapPoints;
173 this->numMapPoints = numMapPoints;
174 this->useMultiMap = true;
175
176 // Extract input values and output positions arrays for multiMapBS
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;
182 }
183 } else {
184 // Use standard linear mapping (no array needed)
185 this->mapPoints = nullptr;
186 this->inputVals = nullptr;
187 this->outputPos = nullptr;
188 this->numMapPoints = 0;
189 this->useMultiMap = false;
190 }
191 }

◆ ~HornetStepper()

HornetStepper::~HornetStepper ( )
inline

Destructor to clean up allocated arrays.

Definition at line 196 of file Hornet_Stepper.h.

196 {
197 if (useMultiMap && inputVals != nullptr) {
198 delete[] inputVals;
199 delete[] outputPos;
200 }
201 }

Member Function Documentation

◆ findZero()

void HornetStepper::findZero ( )
inline

findZero() is a function to zero the gauge

Note
The gauge is zeroed by slowly moving the needle to mechanical zero), then to the dial zero position. You may hear clocking sounds. This is normal.

Coordinate system:

  • Position 0 = mechanical zero (physical stop)
  • Position zeroPos (e.g., 20) = dial zero (where gauge shows "0")

Definition at line 212 of file Hornet_Stepper.h.

212 {
213 int zeroingSpeed = 20; // Slow speed for zeroing operation
214 int zeroingAccel = 10;
215 stepper.setMaxSpeed(zeroingSpeed);
216 stepper.setAcceleration(zeroingAccel);
217
218 stepper.setCurrentPosition(maxPos); // Assume needle at max position
219 stepper.runToNewPosition(mechZero); // Move backwards to mech stop
220 stepper.setCurrentPosition(mechZero); // At mech stop, set coordinate sys to 0
221 stepper.runToNewPosition(zeroPos); // Move forward to position that is 0 on dial
222
223 stepper.setMaxSpeed(normalSpeed); // Resume normal speed and acceleration
224 stepper.setAcceleration(normalAccel);
225 }

Referenced by run().

◆ testFullRange()

void HornetStepper::testFullRange ( int testSpeed = 20,
int testAccel = 10 )
inline

testFullRange() is a function to test the range of the gauge

Note
During the test, the gauge is moved all the way. runToNewPosition() blocks the CPU during test.

Definition at line 233 of file Hornet_Stepper.h.

233 {
234 testInProgress = true;
235 stepper.setMaxSpeed(testSpeed);
236 stepper.setAcceleration(testAccel);
237
238 stepper.runToNewPosition(maxPos);
239 delay(2000);
240 stepper.runToNewPosition(zeroPos);
241
242 stepper.setMaxSpeed(normalSpeed);
243 stepper.setAcceleration(normalAccel);
244 testInProgress = false;
245 }

Referenced by run().

◆ setTarget()

void HornetStepper::setTarget ( unsigned int targetVal)
inline

setTarget() is a function to set the target position of the gauge

Parameters
targetValThe target value as pure DCS BIOS value (value range: 0-65535)

Maps DCS BIOS value to stepper position: 1) cap targetVal at capValue as needed 2) map capped value to stepper position, using multiMapBS if useMultiMap is true, otherwise use standard linear mapping 3) call the moveTo() function of AccelStepper to pass the new target position.

Definition at line 257 of file Hornet_Stepper.h.

257 {
258 // 1) Cap targetVal at capValue as needed
259 unsigned int trimmedVal = min(targetVal, capValue);
260
261 // 2) map capped value to stepper position
262 long targetPos;
263 if (useMultiMap) {
264 // Use multiMap with pre-extracted arrays
265 targetPos = multiMapCache<unsigned int>(trimmedVal, inputVals, outputPos, numMapPoints);
266 } else {
267 targetPos = map(trimmedVal, 0, capValue, zeroPos, maxPos);
268 }
269
270 // 3) call the moveTo() function of AccelStepper to pass the new target position.
271 stepper.moveTo(targetPos);
272 }

◆ run()

void HornetStepper::run ( )
inline

run() is a function to run the stepper motor (non-blocking)

Note
This method must be called repeatedly in the main loop to perform movement. It also checks for button-triggered homing and startup test.

Definition at line 280 of file Hornet_Stepper.h.

280 {
281 bool shouldHome = ufcEntPressed && ufcClrPressed;
282
283 if (shouldHome && !homingInProgress && !testInProgress) {
284 homingInProgress = true;
285 findZero();
287 homingInProgress = false;
288 }
289
290 stepper.run();
291 }
void findZero()
findZero() is a function to zero the gauge
void testFullRange(int testSpeed=20, int testAccel=10)
testFullRange() is a function to test the range of the gauge

References findZero(), and testFullRange().

Member Data Documentation

◆ ufcEntPressed

bool HornetStepper::ufcEntPressed = false
static

Definition at line 123 of file Hornet_Stepper.h.

◆ ufcClrPressed

bool HornetStepper::ufcClrPressed = false
static

Definition at line 124 of file Hornet_Stepper.h.


The documentation for this class was generated from the following file: