LUA script for stepper ENA+ ??

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
jangaloo
Posts: 11
Joined: Sat Mar 28, 2020 9:10 am

LUA script for stepper ENA+ ??

#1 Post by jangaloo »

Evening!

Am working on a new project involving stepper drivers and motors. I would like to use the Ena+ terminal aswell. Is there a way to control that with Lua???

Also while I'm at it - is there some lua that will enable the stepper to use the current position of the hall sensor instead of it moving around?

Thanks!

J

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: LUA script for stepper ENA+ ??

#2 Post by jph »

The ena+ is a simple enable signal, logic 1 or 0 to enable.. depends on the driver. It is only labelled that way on certain drivers - usually the cheap Chinese ti clones It is an ideal use for a simple standard hw ouput add.

As for what you are calling hall effect, you need to explain the setup you have. I presume that you have some kind of pulse interrupt wheel or similar?
You could use a hw input add to read the count but you would need a calibration system to know the start point.
This may be problematic with AM depending on the speed of the stepper rotation and the number or pulses per revolution as you may hit limits in the serial transfer speed and buffer issues depending on what else is occurring. If you re sending out a pulse stream and reading back in a pulse stream then you may have missed instances. The only way would be to try it.
That method would create a closed loop which is good, but many rely on the less accurate pulse counting method - meaning that, as you already know how many step pulses you sent, and you know the step angle and any micro step divider, then you KNOW where you are once the calibration has been done at the beginning so no real need for closed loop.

You really need to explain in detail what you are working with and exactly what you are trying to achieve and not expect people to try to guess or read your mind.. put some effort in.

That may elicit some links for you to go and investigate. ....

For example, look at the api for hw input and output add, stepper api and calibrate functions.
Joe. CISSP, MSc.

jangaloo
Posts: 11
Joined: Sat Mar 28, 2020 9:10 am

Re: LUA script for stepper ENA+ ??

#3 Post by jangaloo »

Fantastic! Thanks very much - ena+ sorted!

As for the Hall sensor part, I´m building a motorised throttle so need the steppers to know exactly where the thrust levers are. Am using an Arduino Mega 2560 with Nema 17 Steppers driven by TB6600 drivers. The thrust levers have hall sensors. Any examples on how the Calibration part should look? I´ve had a look at the API for Calibration but not sure if its going to do what I need it to do. Will have a fiddle later...

Many thanks for your help!

If you need any more details - let me know.

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: LUA script for stepper ENA+ ??

#4 Post by jph »

Hi,
The 'hall sensors' on the thrust levers ? - are they simply 'hall pots' ? - or if not specifically then I presume they output a varying analogue signal depending on angular position. ?
If so, then you can simply feed the analogue signals into the ADC reads of the mega and read the ADC value to determine position accurately.

See 'analog input' under hardware in the following section https://siminnovations.com/wiki/index.p ... _Logic_API
You can also look at 'stepper' under hardware for a sample of calibration but as you seemingly have an analogue position feedback signal available then you simply do not need to calibrate as you will always know where the levers are by looking at the ADC value. A bit of simple experimentation with adc input values will give you a scale but it should - depending on the mechanical layout - be linear so just simple math.

For a motorised throttle it is much better to use a PID system but that is not available in AM at the moment. You 'could' do it with messageport with a PID system but it will be fine without it and it gets much more complex.
You can instead just look at the 'delta' (here meaning rate of change in relation to distance commanded) and slow down or speed up the stepper pulses accordingly so as to avoid jerky throttle movements with small adjustments. At the basic level you simply look at how far you need to move the throttles and use a speed of stepper pulse suitable for the distance where a longer distance will move at the maximum speed that is realistic for the aircraft and a much shorter distance will move the stepper at a slower speed.

How do you plan on handling a manual pilot intervention when the throttles are moving ?

Also, what voltage are you using for the steppers ?
Joe. CISSP, MSc.

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

Re: LUA script for stepper ENA+ ??

#5 Post by Ralph »

PID API function... hmmm ... that would nice! :lol:

SimPassion
Posts: 5336
Joined: Thu Jul 27, 2017 12:22 am

Re: LUA script for stepper ENA+ ??

#6 Post by SimPassion »

 
Nothing really complicated ;-)

image.png
image.png (20.45 KiB) Viewed 832 times

https://en.wikipedia.org/wiki/PID_controller
https://octave.sourceforge.io/control/function/pid.html

 

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

Re: LUA script for stepper ENA+ ??

#7 Post by Ralph »

Haha no :D There are some examples for Lua. But I suppose that it would be better to do this in C.

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: LUA script for stepper ENA+ ??

#8 Post by jph »

Here's some Arduino code I did from 2014 - yikes... 8 years ago.. time flies.

Code: Select all

//PID Basic - 2014
// Joe Hanley
// Basic code for PID loop based on 3DR instructional video

void setup(){
  
}
float pVal = 4.2;              // P of our PID
float iVal = 2.3;              // I of our PID
float dVal = 0.6;              // D of our PID
float Target = 0;              // Where we want to be
float Commulative_Error = 0;   // As per name
float Last_Error = 0;          // Storage location
float Max_Correction = 0;
float Min_Correction = 0;
float Error = 0;
float Current_Value;
float pCorrection = 0;             // calculated P 
float iCorrection = 0;             // calculated I
float dCorrection = 0;             // calculated D
float dSlope = 0;                  // used in dCorrection Caclulation
float Correction;

void loop(){
  Target = Get_Target();  
  Current_Value = Get_Current_Value();
    Error = Target - Current_Value;
    pCorrection = pVal * Error;    // P calculated
    
    Commulative_Error +=  Error; //x=x + y compound addition
    iCorrection = iVal * Commulative_Error;  // I Caclulated
    
    dSlope = Error - Last_Error;
    dCorrection = dSlope * dVal; // D Caclulated
    
    Last_Error = Error;
    Correction = pCorrection + iCorrection + dCorrection;
    if (Correction > Max_Correction){
      Correction = Max_Correction ; }
    if (Correction < Min_Correction){
      Correction = Min_Correction;}
    Apply_Output();
}
float Get_Current_Value(){ // get current position in relation to Target
return 10;
}
float Get_Target(){ // get desired position (IF IT IS A NON CONSTANT - most times it will be a constant!)
}
void Apply_Output(){ // drive output stage
}
Joe. CISSP, MSc.

SimPassion
Posts: 5336
Joined: Thu Jul 27, 2017 12:22 am

Re: LUA script for stepper ENA+ ??

#9 Post by SimPassion »

Ralph wrote: Wed Oct 05, 2022 12:10 pm Haha no :D There are some examples for Lua. But I suppose that it would be better to do this in C.
Yes, this is where the Octave package should help (G++ and Fortran) : https://octave.sourceforge.io/control/index.html
https://octave.sourceforge.io/control/overview.html
https://docs.octave.org/doxygen/stable/

PID Theory : https://msc.berkeley.edu/assets/files/P ... D2-lti.pdf
LTI System : https://en.wikipedia.org/wiki/Linear_ti ... ant_system

pid.m

Code: Select all

## Copyright (C) 2009-2016   Lukas F. Reichlin
##
## This file is part of LTI Syncope.
##
## LTI Syncope is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LTI Syncope is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn{Function File} {@var{C} =} pid (@var{Kp})
## @deftypefnx{Function File} {@var{C} =} pid (@var{Kp}, @var{Ki})
## @deftypefnx{Function File} {@var{C} =} pid (@var{Kp}, @var{Ki}, @var{Kd})
## @deftypefnx{Function File} {@var{C} =} pid (@var{Kp}, @var{Ki}, @var{Kd}, @var{Tf})
## @deftypefnx{Function File} {@var{C} =} pid (@var{Kp}, @var{Ki}, @var{Kd}, @var{Tf}, @var{Ts})
## Return the transfer function @var{C} of the @acronym{PID} controller
## in parallel form with first-order roll-off.
## With a valid @var{Ts} a discrete-time system is created.
##
## @example
## @group
##              Ki      Kd s
## C(s) = Kp + ---- + --------
##              s     Tf s + 1
## @end group
## @end example
## @end deftypefn

## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
## Created: June 2015
## Version: 0.1

## TODO: dozens of options, ...
##       If you wish to kill time with this repetitive task,
##       I'm happy to add your work :-)

function C = pid (Kp = 1, Ki = 0, Kd = 0, Tf = 0, Ts = 0)

  if (! is_real_scalar (Kp, Ki, Kd, Tf, Ts) || nargin > 5 )
    print_usage ();
  endif

  if (Kd == 0)    # catch cases like  pid (2, 0, 0, 3)
    Tf = 0;
  endif

  if (Ki == 0)    # minimal realization if  num(3) == 0  and  den(3) == 0
    C = tf ([Kp*Tf+Kd, Kp], [Tf, 1], Ts);
  else
    C = tf ([Kp*Tf+Kd, Kp+Ki*Tf, Ki], [Tf, 1, 0], Ts);
  endif

endfunction
Last edited by SimPassion on Wed Oct 05, 2022 2:35 pm, edited 3 times in total.

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: LUA script for stepper ENA+ ??

#10 Post by jph »

No reason I can see that you can't do it as is in Lua as it ends up pretty simple as far as coding.

These weren't the original videos I worked from - it is a long time ago - but similar breakdown of the math. There are loads of vids on YT that break it down to manageable levels
The Arduino code should then make sense - the 3 final sub routines (functions) to actually interface your own control need applying of course.

https://ardupilot.org/copter/docs/tuning.html
Joe. CISSP, MSc.

Post Reply