Need some coding help for trainer template

openTx has introduced a range of new features, ideas and bling. It is fast becoming the firmware of choice for many users. openTx will run on ALL current hardware platforms, including the gruvin9x and sky9x boards. Work has already started to support the new FrSky X9D radio!
Post Reply
ReSt
Posts: 1581
Joined: Tue Dec 27, 2011 11:34 pm
Country: -

Need some coding help for trainer template

Post by ReSt »

I need some coding help

I want to setup a template (on TGY9x with M128) , that activates trainer mode for the selected model
With this setup, the student gets control when AIL switch is activ, but trainer takes over control for any of the channels, as soon as he moves a stick out of the given neutral/throttle low position

I already have the template working, setting up the trainer screen

What I do not succeed is how to create the required custom switches and custom functions.


I would like to have the following custom switches:
CS1 |a|<x RUD 5 AIL
CS2 |a|<x ELE 5 AIL
CS3 |a|<x THR -95 AIL
CS4 |a|<x AIL 5 AIL

and the following custom functions:
CS1 TrainRud x
CS2 TrainEle x
CS3 TrainThr x
CS4 TrainAIL x


Reinhard

btw, would a permanent default setup of the trainer page have any negative influence to the overall working conditions of the radio ?

ReSt
Posts: 1581
Joined: Tue Dec 27, 2011 11:34 pm
Country: -

Re: Need some coding help for trainer template

Post by ReSt »

What I found in the meantime
I can set the Custom Switches in a way like that

setSwitch(1,CS_ANEG, MIXSRC_Rud,5);

This gives me: CS1 |a|<Rud 5

But how do I set the switch in column 4 ?? :?:


And what I do not get at all is, how to set the Custom Functions as a trainer switch? :?:

I want this:
CS1 TrainerRud x

I have no idea how to realize that.

Any help will be welcome.

Reinhard
User avatar
Kilrah
Posts: 11109
Joined: Sat Feb 18, 2012 6:56 pm
Country: Switzerland

Re: Need some coding help for trainer template

Post by Kilrah »

Neither setting the AND switch nor adding custom functions from a template are implemented as they were never needed.
You'd need to extend setSwitch and create a similar function for the CFs too.
btw, would a permanent default setup of the trainer page have any negative influence to the overall working conditions of the radio ?
None apart from the fact that if no trainer radio is connected and you flip the AIL switch you'll lose control of the model until you flip it again.
ReSt
Posts: 1581
Joined: Tue Dec 27, 2011 11:34 pm
Country: -

Re: Need some coding help for trainer template

Post by ReSt »

Thanks for the response Kilrah,

but in the normal code there is the ability to set the switch that finally shows up as a switch in the 4th column of the custom switch.
This information is stored somewherein a variable and finally in the EEprom.
So why can't I directly address this variable and set the switch?

My problem is, that I'm no C++ programmer and simply don't know, how to address that variable.
Everything that I tried ends up with some compile errors.

Reinhard
User avatar
Kilrah
Posts: 11109
Joined: Sat Feb 18, 2012 6:56 pm
Country: Switzerland

Re: Need some coding help for trainer template

Post by Kilrah »

setSwitch() is what does the interface between templates and the underlying data structure. It doesn't offer a way to set the AND switch, simply because the AND switch was added much later and it was never needed to set it from a template.

So you need to modify setSwitch() to give it that capability.

Code: Select all

void setSwitch(uint8_t idx, uint8_t func, int8_t v1, int8_t v2)
{
  CustomSwData *cs = cswAddress(idx-1);
  cs->func = func;
  cs->v1   = v1;
  cs->v2   = v2;
}
You see it references the CS by its address.

As can be found in myeeprom.h, on stock a custom switch has these elements:

Code: Select all

PACK(typedef struct t_CustomSwData { // Custom Switches data
  int8_t  v1; //input
  int8_t  v2; //offset
  uint8_t func:4;
  uint8_t andsw:4;
}) CustomSwData;
andsw is a 4-bit value that can be one of the first 16 elements of enum SwitchSources (in myeeprom.h), see there for the order.

So you could modify setSwitch to add the andsw:

Code: Select all

void setSwitch(uint8_t idx, uint8_t func, int8_t v1, int8_t v2, uint8_t andsw)
{
  CustomSwData *cs = cswAddress(idx-1);
  cs->func = func;
  cs->v1   = v1;
  cs->v2   = v2;
  cs->andsw = andsw;
}
and then call it as
setSwitch(1,CS_ANEG, MIXSRC_Rud,5, SWSRC_AIL);

Then create a function similar to setSwitch for the CFs.

ReSt
Posts: 1581
Joined: Tue Dec 27, 2011 11:34 pm
Country: -

Re: Need some coding help for trainer template

Post by ReSt »

Thanks, Kilrah.
I think that info will help me a lot.

Reinhard
ImRich
Posts: 55
Joined: Sat Apr 05, 2014 9:25 pm
Country: -

Re: Need some coding help for trainer template

Post by ImRich »

ReSt,

Do you mind if I ask? This sounds very similar to this other thread:
[IDEA] Smart Trainer switch
viewtopic.php?f=45&t=5316&start=30#p75595

Is this similar to what you are trying to do here?
ReSt
Posts: 1581
Joined: Tue Dec 27, 2011 11:34 pm
Country: -

Re: Need some coding help for trainer template

Post by ReSt »

At first, I must admit, that I have not yet used my radio as a trainer. I simply was trying of how to setup the trainer/student mode.

Yes, I think, that, what I tried to create, is similar to what was discussed in the other thread. But obviously, teachers have their personal idea of how trainer/student mode should work. And it may also depend on what models you try to train, glider, heli, ..copters or what ever.

My idea behind this is, that the student flies the plane and the teacher at any moment, can override the students signal on any (and any combination) of the four stick channels. That means, the teacher gets control on a stick axis, as soon as he moves a stick out of its default positition (neutral for AER and low for Thr). And the teacher does not "fly the plane" all the time and the student only has control as long as he has the same stick positions as the teacher. (because this would be the consequence if I compare the teachers and the students actual stick values and give the teacher control as soon as the difference is above a given value).
The disadvantage pobably will be, that a servo where the teacher takes control, will do a short jump to the near default value of the teacher, in the moment when he takes control.

I got my solution as a template working for me on my radio setup (stick and channel order) in ER9x as well as in OpenTx.
In Er9x it set up the trainer page and the necessary four custom switches(1-4) and sets the trainer switch to ON.
In OpenTx it sets up ther trainer page and the four required custom switches(1-4) and four custom functions(1-4) that activate the trainer functions for each of the four axis.


It is possible, that it does not immediately work on other combinations of stick and channel order. But that could be easily adapted in the code. Or somebody who has more knowledge of the radio and C++ could create a universal solution (if there is enough interest).

Reinhard
ImRich
Posts: 55
Joined: Sat Apr 05, 2014 9:25 pm
Country: -

Re: Need some coding help for trainer template

Post by ImRich »

Reinhard,

Ok, it's good to know you're trying something similar to the ideas in the other thread. You may want to read the thread as it has many good ideas by others.

I would offer my thoughts, that capturing the instructor's stick positions at the time of pressing the training switch is much more useful than just assuming they will be always centered for Aileron, Elevator, Rudder, and at low Throttle. You also want some 'slack' (hysterisis?) in this captured values so that any slight variation from vibrations (.etc) in the instructor's transmitter doesn't cause the student to lose control when it is not expected.

If I understand you correctly, I'm not sure how useful it will be to compare the students stick's position to those of the instructor's. Or perhaps I don't understand what you are trying to accomplish.

As to the servo's jumping when the instrutor takes over, that's ok, it means he's trying to save the aircraft from crashing! :)

I hope you don't mind that I'll add a link to this thread in the other thread to show interest in the general ideas presented in the other thread.

Good luck with your project. I sure with that the OpenTX community would consider my idea for an easy to enable check box.

Regards,
Rich
User avatar
MikeB
9x Developer
Posts: 17993
Joined: Tue Dec 27, 2011 1:24 pm
Country: -
Location: Poole, Dorset, UK

Re: Need some coding help for trainer template

Post by MikeB »

Just used eepe to try this:

CH15: +100% CH15 Switch(!ELE)
+100%THR Switch(ELE)
CH16: +100% CH15
-100% THR

CS1: |v|>ofs CH16 7

Channel 15 either follows the THR stick (ELE ON) or holds the value (ELE OFF)
Channel 16 has the difference between channel 15 and the THR stick, 0 if ELE ON, but therefore detects movement if ELE OFF.
CS1 turns on when channel 16 indiates the THR stick has been moved since ELE switch went OFF.

Mike.
erskyTx/er9x developer
The difficult we do immediately,
The impossible takes a little longer!
ReSt
Posts: 1581
Joined: Tue Dec 27, 2011 11:34 pm
Country: -

Re: Need some coding help for trainer template

Post by ReSt »

Thanks for all these thoughts, but I think I found the solution that will fit my needs.

I activate the trainer/student functions with a 2pos switch, in my case with the Ail switch.
As long as the switch is off, only the trainer has control.
When the switch is on, the student has control for all four stick axis, as long as the teacher does not move his sticks out of the default position (that is neutral [+-4] for Rud, Ele, Ail and low for Thr)
If the Teacher doesnt touch the sticks, the student has control.
As soon, as the teacher moves a stick out of the default position, he has control for that axis and the student still controls the remaining axis.
The amout of the hysteresis is set up in the switch settings as e.g. <5 (%).

In OpenTx there are the Custom Functions "Trainer", that activates the trainer function for all sticks and the Custom Functions "TrainerRud", "TrainerEle", TrainerThr" and "TrainerAil" that activate the trainer function for one single stick axis each.

I'm using the TrainerRud,.... functions to activate the trainer function for every single axis with a custom switch.
for example the switch for elevon:
S2 |x| < Ele 5 AIL

and the function
CS2 TrainerEle x

If AIL switch is active AND the absolute value of the trainers ELE stick is less than 5 than CS2 activates the TrainerEle function that has been activated (x)

And the same is true for the other axis

Reinhard
User avatar
jhsa
Posts: 19480
Joined: Tue Dec 27, 2011 5:13 pm
Country: Germany

Re: Need some coding help for trainer template

Post by jhsa »

Reinhard, Imagine your student did something wrong on elevator and you took control to correct it. How do you give control back to him on that axis after stabilizing the plane?

João

Sent from my GT-I9195 using Tapatalk
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9

Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
User avatar
Kilrah
Posts: 11109
Joined: Sat Feb 18, 2012 6:56 pm
Country: Switzerland

Re: Need some coding help for trainer template

Post by Kilrah »

Simply lets go of the stick again. Anytime the stick is centered the student has control as long as the trainer switch is on.
ReSt
Posts: 1581
Joined: Tue Dec 27, 2011 11:34 pm
Country: -

Re: Need some coding help for trainer template

Post by ReSt »

Thats exactly the idea behind it. :D

Reinhard

Post Reply

Return to “openTx”