hw_stepper_motor_calibrate is moving the stepper

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

Post Reply
Posts: 2
Joined: Fri Sep 29, 2023 7:48 pm

hw_stepper_motor_calibrate is moving the stepper

#1 Post by y24jds@gmail.com »

I have the following:
28byj-48 stepper
uln20003 driver board
arduino 2560 mega

I am building an airspeed indicator and everything is coming together well except I am seeing odd behavior that I can't wrap my head aruond.

I implemented a stop at the far end of the scale at what would be about 20 knots. Here is my process:

Rotate endless counterclockwise
wait 4 seconds
hw_stepper_motor_calibrate(stepper_airspeed, 0.0) <-- this should set the 0 point at its current position
hw_stepper_motor_position(stepper_airspeed, 0.0 ) <-- this should stop the endless rotation and leave it at 0.0
wait 4 seconds
hw_stepper_motor_position(stepper_airspeed,0.1) <-- this aligns the needle to my lowest value on the scale which is 40 knots for the Cessna 172
wait 4 seconds
hw_stepper_motor_calibrate(stepper_airspeed, 0.0) <-- this is suppose to reset the 0.0 calibration point to the needles current position which is sitting on 40 knots

But instead of resetting the 0 point there, the needles goes off and moves like 30 knots in the increasing direction.

Why would a second call to calibrate cause the motor to spin? And the amount it spins doesnt even make sense, but it shouldnt spin at all. I am just trying to set the floor of my movement range. Movement on a stepper should be 0.0 to 1.0. And that should corespond to 40-200 knots.

What am I missing here? What is wrong with the call to calibrate??

I can share my whole program if you want, but I am single stepping it and it is very straightforward and I have 23 years of programming experience for my job. SO I thought I know what I'm doing..

Thanks so much!

User avatar
Posts: 7652
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg

Re: hw_stepper_motor_calibrate is moving the stepper

#2 Post by Ralph »

Can you please post your code between the code brackets?

Posts: 2
Joined: Fri Sep 29, 2023 7:48 pm

Re: hw_stepper_motor_calibrate is moving the stepper

#3 Post by y24jds@gmail.com »

Here is the whole code right now. Note: I have commented out the part for now that gets the airspeed from MSFS. I'm not sure that will work as I have it because that block of code was in a different variation I was working with that did work, but I refactored the program using these various functions with timer_start's in between them with delays. I needed some delay to give time for the needle to reset to the stop, time to process the calibration and initial move, etc..

The issue is on Step3_ZeroAt40. As soon as it hits that hw_stepper_motor_calibrate(stepper_airspeed,0.0), the needle MOVES upward. It doesnt do that the first time I call calibrate within Step1. Is there some weird issue in a single program if you are calling calibrate multiple times or something?

I am single step debugging each of these and the functions that have pauses in between them seem to work fine. Every command seems to do what it needs to do - with the exception of that second calibrate... Very odd...

Anyways, appreciate more eyes on this. This is my FIRST attempt at using Air Manager and writing the LUA scripts. Thanks!

Remember to ignore the block of commented out code for now, I still have to work that out.

Code: Select all

local function Step3_ZeroAt40()
   print ("Step3 - Zero At 40")

local function Step2_Move40()
   print ("Step2 - Move to 40")

local function Step1_InitialCalibration()
   print ("Step1 - Zero indicator on stop")
   hw_stepper_motor_calibrate(stepper_airspeed, 0.0) 
   hw_stepper_motor_position(stepper_airspeed, 0.0 )

local function Step0_Initialization()
  local input_min=40
  local input_max=200
  local output_min=0
  local output_max=1
  stepper_airspeed = hw_stepper_motor_add("4WIRE_4STEP", 2038, 20, true, "ARDUINO_MEGA2560_A_D8", "ARDUINO_MEGA2560_A_D10", "ARDUINO_MEGA2560_A_D9", "ARDUINO_MEGA2560_A_D11")
 -- stepper_airspeed = hw_stepper_motor_add("airspeed motor","4WIRE_4STEP",2048,20)
  hw_stepper_motor_position(stepper_airspeed, nil, "ENDLESS_COUNTERCLOCKWISE")

-- local function new_airspeed_callback(airspeed)
--   local normalised_input= (airspeed-input_min) / (input_max - input_min)
--   local output = output_min+(normalised_input * (output_max - output_min))
--   print(output)
--   print("The airspeed is: " .. airspeed)
--   if airspeed>=40 then
--   print ("sending to servo")
--     hw_stepper_motor_position(stepper_airspeed,output)
--   end
-- end

-- Step1_InitialCalibration()
-- fs2020_variable_subscribe("AIRSPEED INDICATED","Knots",new_airspeed_callback)

Post Reply