Viggen airspeed gauge
Hello!
My first post in here, so please bear with me :) I'm planning on building a Viggen simulator. I have been playing around with different switches and stepper motors. Now I have run into trouble though when trying to make an X27.168 stepper motor show the airspeed in the Viggen. I borrowed some code from this forum and tried to tweak it. The motor does its thing in the beginning by finding the left end point. But when I try to run it with DCS and click Fly after loading the game, it moves to the right end position, and then counts "backwards". As if the 0 position is to the right. I hope that you understand what I'm talking about.
I think the problem lies in the first part when the stepper is finding the 0 position.
Can someone please help me figure out what i'm doing wrong?
/*
Tell DCS-BIOS to use a serial connection and use interrupt-driven
communication. The main program will be interrupted to prioritize
processing incoming data.
This should work on any Arduino that has an ATMega328 controller
(Uno, Pro Mini, many others).
*/
#define DCSBIOS_IRQ_SERIAL
#include "AccelStepper.h"
#include "DcsBios.h"
struct StepperConfig {
unsigned int maxSteps;
unsigned int acceleration;
unsigned int maxSpeed;
};
class Vid29Stepper : public DcsBios::Int16Buffer {
private:
AccelStepper& stepper;
StepperConfig& stepperConfig;
unsigned int (*map_function)(unsigned int);
unsigned char initState;
public:
Vid29Stepper(unsigned int address, AccelStepper& stepper, StepperConfig& stepperConfig, unsigned int (*map_function)(unsigned int))
: Int16Buffer(address), stepper(stepper), stepperConfig(stepperConfig), map_function(map_function), initState(0) {
}
virtual void loop() {
if (initState == 0) { // not initialized yet
stepper.setMaxSpeed(stepperConfig.maxSpeed);
stepper.setAcceleration(stepperConfig.acceleration);
stepper.moveTo(-((long)stepperConfig.maxSteps));
initState = 1;
}
if (initState == 1) { // zeroing
stepper.run();
if (stepper.currentPosition() <= -((long)stepperConfig.maxSteps)) {
stepper.setCurrentPosition(0);
initState = 2;
stepper.moveTo(stepperConfig.maxSteps);
}
}
if (initState == 2) { // running normally
if (hasUpdatedData()) {
unsigned int newPosition = map_function(getData());
newPosition = constrain(newPosition, 0, stepperConfig.maxSteps);
stepper.moveTo(newPosition);
}
stepper.run();
}
}
};
/* modify below this line */
/* define stepper parameters
multiple Vid29Stepper instances can share the same StepperConfig object */
struct StepperConfig stepperConfig = {
650, // maxSteps
1000, // maxSpeed
1000 // acceleration
};
// define AccelStepper instance
AccelStepper airspeedstepper(AccelStepper::FULL4WIRE, 13, 12, 11, 10);
// define AdafruitStepper class that uses the AccelStepper instance defined in the line above
// +-- arbitrary name
// | +-- Address of stepper data (from control reference)
// | | +-- name of AccelStepper instance
// v v v v-- StepperConfig struct instance
Vid29Stepper airspeed(0x4676, airspeedstepper, stepperConfig, [](unsigned int newValue) -> unsigned int {
/* this function needs to map newValue to the correct number of steps */
return map(newValue*50, 0, 65000, 0, stepperConfig.maxSteps);
});
void setup() {
DcsBios::setup();
}
void loop() {
DcsBios::loop();
}