Jump to content

Need help with code for x27 stepper motor (Solved)


Recommended Posts

Posted (edited)

Hi everyone!

I used code below for my Var-30 (Black Shark 3 KA-50)

It works but not so good as I want. Randomly my "gauge" (arduino uno r3 + easydriver + x27 168 stepper motor) is starting to reboot. ((

It could be once or many times during the flight (~one hour)

I changed the power source from USB and external adapter (12V). It didn't work.

Wired 5V and RES pins on Arduino. Also didn't work.

Help me please!

 

#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/2);
       }
     }
     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 = {
 3600,  // maxSteps
 1000, // maxSpeed
 10000 // acceleration
 };


// define AccelStepper instance
AccelStepper stepper(AccelStepper::DRIVER, 11, 10);
// define Vid29Stepper 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 vvi(0x184e, stepper, stepperConfig, [](unsigned int newValue) -> unsigned int {
 /* this function needs to map newValue to the correct number of steps */
 return map(newValue, 0, 65535, 0, stepperConfig.maxSteps);
}); 


void setup() {
 DcsBios::setup();
}

void loop() {
 DcsBios::loop();
}
 
Edited by Vzhik
Posted (edited)

@Vzhik

Is your stepper motor repeatedly going to zero position or is the Uno rebooting inflight?

Are you sure, that the gauge address 0x10a0 is correct?

A look into the dcs-bios-arduino-library reveals hex code 0x184E for the Ka_50_VARIO_SPEED.

 

If the problems remain, you may remove the stepper object and start separate coding for each stepper motor.

Pls find below an example for a two-stepper gauge:

Spoiler
/*
  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 "DcsBios.h"
#include <Wire.h>
#include <SPI.h>
#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper_oxy(AccelStepper::DRIVER,5,4);  // Defaults to AccelStepper::DRIVER (2 pins) on STEP and DIR
AccelStepper stepper_pres(AccelStepper::DRIVER,6,7);  // Defaults to AccelStepper::DRIVER (2 pins) on STEP and DIR

int stepperPosition_oxy;
int stepperPosition_pres;

/* setup the Stepper motors */
void stepperinit() {

// first for the Oxygen Gauge  
  stepper_oxy.setMaxSpeed(3000); // best velocity found
  stepper_oxy.setAcceleration(1500); // best accelerartion found
  stepper_oxy.runToNewPosition(630); // end position of motor movement
  stepper_oxy.setCurrentPosition(630); // set end position
      stepper_oxy.run();  
// second for the Pressure Gauge
  stepper_pres.setMaxSpeed(3000); // best velocity found
  stepper_pres.setAcceleration(1500); // best accelerartion found
  stepper_pres.runToNewPosition(630); // end position of motor movement 
  stepper_pres.setCurrentPosition(630); // set end position
      stepper_pres.run();

  delay(500);

  stepper_oxy.runToNewPosition(0); // go to start position
  stepper_pres.runToNewPosition(0); // go to start position  
    while (stepper_pres.distanceToGo() != 0 ) 
    {
      stepper_oxy.run();
      stepper_pres.run();
    }
  delay(500);

  stepper_oxy.moveTo(360); // max value of the scale (5 Liter Oxy) = 180° Turn
  stepper_pres.moveTo(600); // max value of the scale (50 x 1000) = 300° Turn

    while (stepper_pres.distanceToGo() != 0 ) 
    {
      stepper_oxy.run();
      stepper_pres.run();
    }
  delay(500);
  
  stepper_oxy.moveTo(0); // min value of the scale (0 PSI)
  stepper_pres.moveTo(0); // min value of the scale (0 x 1000)

    while (stepper_pres.distanceToGo() != 0 ) 
    {
      stepper_oxy.run();
      stepper_pres.run();
    }
}

/* paste code snippets from the reference documentation here */

/* Oxygen Pressure Indicator: Max. Value: 65535 Description: gauge position 5 Liter Liquid Oxygen */
void onOxyVolumeChange(unsigned int newValue) {
    stepperPosition_oxy = map(newValue,0,65535,0,720);
  if (stepperPosition_oxy < 0) {stepperPosition_oxy = 0;}
  else if (stepperPosition_oxy > 360) {stepperPosition_oxy = 359;}
  else {
  stepper_oxy.moveTo(stepperPosition_oxy);}
}
DcsBios::IntegerBuffer oxyVolumeBuffer(0x1132, 0xffff, 0, onOxyVolumeChange);

/* Cabin Air Pressure Indicator: Max. Value: 65535 Description: gauge position 50 x 1000 Pressure Altitude */
void onCabinPressAltChange(unsigned int newValue) {
    stepperPosition_pres = map(newValue,0,65535,0,720);
    stepper_pres.moveTo(stepperPosition_pres);
}
DcsBios::IntegerBuffer cabinPressAltBuffer(0x1134, 0xffff, 0, onCabinPressAltChange);



void setup() {
  DcsBios::setup(); // Setup DCS-BIOS dont change !!
  stepperinit(); // call the Stepper Motor and set it to 0
}

void loop()
{    
  DcsBios::loop();
  stepper_oxy.run();
  stepper_pres.run();
}

 

 

You also may get rid of the driver board and use the direct connection to check if the problem is still there.

AccelStepper stepper(AccelStepper::FULL4WIRE,4,5,6,7);  // stepper on pins 4, 5, 6 and 7

That should be a much simpler approach for a single stepper gauge.

 

Regards, Vinc

Edited by Vinc_Vega
  • Like 1

Regards, Vinc

real life: Royal Bavarian Airforce

online: VJS-GermanKnights.de

[sIGPIC][/sIGPIC]

Posted (edited)

@Vinc_Vega

Hi, Vinc!

Many thanks for you answer!

В 17.04.2024 в 19:23, Vinc_Vega сказал:

Is your stepper motor repeatedly going to zero position or is the Uno rebooting inflight?

It's rebooting only inflaght.

В 17.04.2024 в 19:23, Vinc_Vega сказал:

Are you sure, that the gauge address 0x10a0 is correct?

A look into the dcs-bios-arduino-library reveals hex code 0x184E for the Ka_50_VARIO_SPEED.

Offcourse it's 0x184E hex code. Sorry. My mistake ))

В 17.04.2024 в 19:23, Vinc_Vega сказал:

You also may get rid of the driver board and use the direct connection to check if the problem is still there.

AccelStepper stepper(AccelStepper::FULL4WIRE,4,5,6,7);  // stepper on pins 4, 5, 6 and 7

That should be a much simpler approach for a single stepper gauge.

Seems like the problem was with the driver board. Changed code with your recomendation. The Var-30 is now working prefect!

Thank you very much!

Have nice flights!

Edited by Vzhik
  • Thanks 1
  • Udav_Kaa changed the title to Need help with code for x27 stepper motor (Solved)
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...