DCS-Skunkworks / dcs-bios

Data export tool for DCS.
https://dcsbios.com/
GNU General Public License v3.0
289 stars 65 forks source link

F18 LTD/R 2position Switch Bugged #134

Closed Lowlyslows closed 8 months ago

Lowlyslows commented 1 year ago

Hello,

After much trial and error trying to get the F18 LTD/R switch to turn off when I turn my physical switch off I believe I found a bug. I am using flightpanels DCS-BIOS. When I open the control reference to "toggle" the LTD/R it moves to the ARM position. If I toggle it again it doesn't move. The LST/NFLR moves to on and off when I press the toggle button. If I click on INC the LTD/R switch toggles between Arm and Safe. DEC doesn't do anything. If I click on INC for the LST/NFLR it moves to ON only and if I click DEC it moves to off. It appears that the LTD/R logic is bugged. If I wire my switch to the LST/NFLR and use the same Switch2Pos code but for the LST, the switch will turn on when I move my physical switch to ON and off when I move my physical switch to OFF. LTD/R does not behave this way, it stays on when I turn my physical switch off.

The F18 has auto lasing. So when you do a bomb run the jet calculates the last 15-20 seconds till impact and then starts lasing. Once the bomb impacts the computer kills the power to the magnet and the switch (LTD) moves back to safe. I am trying to replicate that. Not sure if fixing the on/off (arm/safe) to behave correctly will get me to my goal but it’s a start.

thanks!

charliefoxtwo commented 1 year ago

Line in question: https://github.com/DCSFlightpanels/dcs-bios/blob/018b13a440381842cc02a672c3e805f08f719082/Scripts/DCS-BIOS/lib/FA-18C_hornet.lua#L1112

I don't know for certain, but I suspect it should be defined as defineElectricallyHeldSwitch similar to the A-10's SAS switches https://github.com/DCSFlightpanels/dcs-bios/blob/018b13a440381842cc02a672c3e805f08f719082/Scripts/DCS-BIOS/lib/A-10C.lua#L706-L709

@Lowlyslows , have you tried any other electrically-held switches? Would you happen to know all of the electrically-held switches in the aircraft?

I'm also not too familiar with the aircraft. Could you provide steps to engage the switch? I've tried master arm on, A/G on, flir on, but it still doesn't hold - this was just in a free flight instant action though. Do I need any specific armament or pods?

WarLord211 commented 1 year ago

@charliefoxtwo wont work like in the A-10 because the 2 command is missing.

@Lowlyslows wich BIOS version do u use?

Lowlyslows commented 1 year ago

@WarLord211 using the latest and greatest bios from DCS flight panels. Installed everything yesterday.

Lowlyslows commented 1 year ago

@charliefoxtwo hey! Yea I bet you need to have the pod install (station 5). I can get the electromagnet to engage but might just be lucky with code example I stole from the warthog project. If you check that site, you’ll see the magnetic switch I copied. Works well!

charliefoxtwo commented 1 year ago

@charliefoxtwo wont work like in the A-10 because the 2 command is missing.

@WarLord211 what do you mean by "the 2 command"?

Do you mean the second number? Like the F18 only has 3002 but the A10 has 3009 and 3010 for a single command?

WarLord211 commented 1 year ago

exactly by A10 the 2. number is the release command.

Lowlyslows commented 1 year ago

@WarLord211 I’m confused. Is there something I need to do that’s similar to the A10?

charliefoxtwo commented 1 year ago

This is a limitation of the DCS module, so there's not much you can do. I'm trying to find a way to work around it.

Lowlyslows commented 1 year ago

@charliefoxtwo what do you mean?

charliefoxtwo commented 1 year ago

So as the A-10 is programmed, for switches like the SAS switch there are actually two separate commands, one for on and one for off. For the F-18, it seems there's only one command for toggling the switch. Again, this has to do with the module itself, nothing on the bios side. We're just interfacing with the module.

Are there any other electrically-held switches in the hornet? I know the engine start is a 3 pos electrically held switch but that's it.

Lowlyslows commented 1 year ago

@charliefoxtwo I believe the APU switch is. Once the engines are running the switch turns off. I’ll look for more, I think the launch bar or anti skid is as well. Very bummed

charliefoxtwo commented 1 year ago

That's very helpful. Can you open up lib/FA-18C_hornet.lua and replace line 1112 with the below line and let me know if that solves your problem?

defineToggleSwitchToggleOnly2("LTD_R_SW", 62, 3002, 441, "Sensor Panel", "LTD/R Switch, ARM/SAFE") 

I'm not sure exactly what you should do for the arduino code though. Presumably one of the following?

const byte ltdRSwPins[2] = {PIN_0, PIN_1};
DcsBios::SwitchMultiPos ltdRSw("LTD_R_SW", ltdRSwPins, 2);
DcsBios::Switch2Pos ltdRSw("LTD_R_SW", PIN);
Lowlyslows commented 1 year ago

@charliefoxtwo is that C:\Users\user\Saved Games\DCS.openbeta\Scripts\DCS-BIOS\lib?

I tested the APU switch and it works. Need to run the code for the electromagnet and see if it holds and then shuts off when the game says for it to. I'll have to try that later though. Thanks

Lowlyslows commented 1 year ago

@charliefoxtwo changing that line worked! I need to test the magnet next!!

Lowlyslows commented 1 year ago

@charliefoxtwo ok, we are 95%!!! I can now toggle the switch correctly. When I turn the physical switch ON the LTD/R switch turns ON. When I turn the physical switch OFF the LTD/R switch turns OFF. When I turn the physical switch on my electromagnet turns ON and holds the physical switch ON. When I turn the physical OFF the electromagnet turns OFF. When the physical switch is ON (LTD/R switch is ARM) and turn the LTD/R switch (game switch) to SAFE the electromagnet turns OFF and the physical switch turns OFF (pretty cool).

What still needs to be made to work is when I turn the FLIR to STBY or OFF in the game, the LTD/R switch goes from ARM to SAFE. However, if I have the physical switch turned to ON (LTD/R is ARM) and I turn the FLIR OFF (in game), the switch doesn't de-magnetize and turn OFF.

Why this is important on top of the above is when the F18 does an auto bombing run the laser resets to SAFE after the run is done. I'd like to have this happen if possible.

Below is my code:

define DCSBIOS_IRQ_SERIAL

include "DcsBios.h"

int Coil = 6; // Magnetic coil on

DcsBios::Switch2Pos ltdRSw("LTD_R_SW", 2); //LTD ARM/SAFE DcsBios::Switch2Pos lstNflrSw("LST_NFLR_SW", 3); //LST ON/OFF DcsBios::Switch3Pos flirSw("FLIR_SW", 4, 5); //FLIR ON/STBY/OFF

DcsBios::Switch2Pos hookLever("HOOK_LEVER", 12); //HOOK up/down

// LTD void onLtdRSwChange(unsigned int newCoilValue) { //Controls the output of pin 6 and turns on electromagnet switch (newCoilValue){ case 0: digitalWrite(Coil, LOW); break; case 1: digitalWrite(Coil, HIGH); break; }
}

DcsBios::IntegerBuffer ltdRSwBuffer(0x74c8, 0x4000, 14, onLtdRSwChange);

void setup() { DcsBios::setup(); pinMode (Coil, OUTPUT); }

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

Lowlyslows commented 1 year ago

Just tested the logic. I set the LTD/R (game switch to ARM). Doing so turned ON the electromagnet (the physical switch was set to OFF). I did an auto bombing run and after the bomb struck the target (about 3-5 seconds) the LTD/R (game switch) went to SAFE. When it did this it also turned OFF the electromagnet.

Something about the physical switch being ON is preventing the game logic from turning off the power to the electromagnet.

charliefoxtwo commented 1 year ago

When the physical switch is ON (LTD/R switch is ARM) and turn the LTD/R switch (game switch) to SAFE the electromagnet turns OFF and the physical switch turns OFF (pretty cool).

Something about the physical switch being ON is preventing the game logic from turning off the power to the electromagnet.

These two statements seem to conflict with each other 🤔

What still needs to be made to work is when I turn the FLIR to STBY or OFF in the game, the LTD/R switch goes from ARM to SAFE. However, if I have the physical switch turned to ON (LTD/R is ARM) and I turn the FLIR OFF (in game), the switch doesn't de-magnetize and turn OFF.

When you do this, what is the value of the LTD/R switch in the bios reference tool?

Lowlyslows commented 1 year ago

@charliefoxtwo It's a bit confusing to write, ha!. The "Something about..." has to do with the text right above it. Essentially if I manually turn the LTD/R switch from ARM to SAFE then my electromagnet will de energize. If the game calls to logically turn the game LTD/R switch from ARM to SAFE then the code wont de energize my electromagnet. Again, something about the physical switch being ON is preventing the game logic from turning off the power to the electromagnet.

Not sure how to check the BIOS reference, how do I do this? Is this the Chrome thing?

charliefoxtwo commented 1 year ago

Yup, you can check with the chrome tool or with my tool, both work (I believe it's under the "Sensors" category).

So by "game logic" you mean like when the switch is deactivated indirectly (not by clicking on it, but rather by some other state changing, e.g. master arm off, a/g mode off, flir off, etc)?

Lowlyslows commented 1 year ago

@charliefoxtwo yep, that’s exactly what I mean! If I get a chance tomorrow I’ll check Chrome tool.

Also thank you so much for helping me.

Lowlyslows commented 1 year ago

Looks like there are several other switches that are electronically held.

https://forum.dcs.world/applications/core/interface/file/attachment.php?id=172025

WarLord211 commented 1 year ago

should i check the changed line in?

charliefoxtwo commented 1 year ago

@WarLord211 still looking into the other issue that was mentioned. I'll submit a PR once everything is ready :)

Lowlyslows commented 1 year ago

@charliefoxtwo @WarLord211: Using the Chrome Reference Tool with game switches only: When I set the LTD/R (game switch) switch to ARM the Output type: integer Current Value is 1 (100%). With it set to SAFE its 0 (0%). When the FLIR (game switch) is set to ON the Current value is 2 (100%), STBY is 1 (50%), and OFF is 0 (0%). When LTD/R (game switch only) is set to ARM and I set the FLIR to STBY the LTD/R current value goes from 1 (100%) to 0 (0%), the FLIR when set to STBY goes from 2 (100%) to 1 (50%).

Using the Chrome Reference Tool with my (physical) switch: When I set the LTD/R physical switch to ARM the current value is 1 (100%) and is being magnetically held at ARM. When I toggle the FLIR (game switch) switch to STBY the FLIR current goes to 1 (50%) but the LTD/R current remains at 1 (100%).

Lowlyslows commented 1 year ago

@charliefoxtwo I also confirmed that the APU acts exactly the same way when it should release/de-energize the electromagnet on my physical switch when the APU light goes out/is commanded off in game. Cheers

Lowlyslows commented 1 year ago

@charliefoxtwo I tested the A10C for the L SAS switch. I don't know when it logically turns off the switch but I did find something interesting. Everything between the F18 and the A10C functions the same both in and out of the game with all of the above. However, both planes when the generators are turned off de-magnetize the switches (in game). Oddly enough, when I turn off the generators on the A10C my physical switch turns off (electromagnet de-energized). When I turn off the L GEN on the F18, my physical switch remains ON (electromagnet energized).

The other thing to note on the A10C is that when the Emergency Disconnect Lever button is pressed all of the SAS switches turn to off. When I have my physical switch turned on (electromagnet energized) and I hit the emergency disconnect lever key bind, my electromagnet de-energizes.

Don't know if that test helps at all.

Merry Christmas

Code I used (same as the F18)

define DCSBIOS_IRQ_SERIAL

include "DcsBios.h"

int SaspYawSasL_Coil = 6; // Magnetic coil

DcsBios::Switch2Pos saspYawSasL("SASP_YAW_SAS_L", 2);

// Yaw SAS Left void onSaspYawSasLChange(unsigned int newCoilValue) { switch (newCoilValue){ case 0: digitalWrite(SaspYawSasL_Coil, LOW); break; case 1: digitalWrite(SaspYawSasL_Coil, HIGH); break; }
}

DcsBios::IntegerBuffer saspYawSasLBuffer(0x1108, 0x0400, 10, onSaspYawSasLChange);

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

pinMode (SaspYawSasL_Coil, OUTPUT); }

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

charliefoxtwo commented 1 year ago

The other thing to note on the A10C is that when the Emergency Disconnect Lever button is pressed all of the SAS switches turn to off. When I have my physical switch turned on (electromagnet energized) and I hit the emergency disconnect lever key bind, my electromagnet de-energizes.

This is in the A-10, right? Unfortunately since the A-10 and F-18 are different modules even though you might think the behavior would be the same (especially since they're both by ED), that's not guaranteed to be the case.

Are there any switches in the F-18 that properly de-energize the electromagnet when they are disabled via game logic? My understanding right now is that you haven't found an F-18 switch that will de-energize your electromagnet when the physical switch is on and the switch is turned off via game logic (not by clicking, but by some other action) - is that correct?

Lowlyslows commented 1 year ago

@charliefoxtwo id have to run tests on all of them which I am willing to do. Your statement is correct. So far I have tested the LTD and the APU switches. APU is interesting as when the game logic tries to turn off the APU switch (after both engines are running) the switch in game stays on but the LED turns off (physical switch remains ON and magnetized thus keeping the game APU switch ON).

Merry Christmas!

charliefoxtwo commented 1 year ago

I know it's a bit of work but it would be really helpful if you were able to. No rush, I'll be busy the next couple days but I should have a bit of free time later in the week to dig deeper and see if we can figure out any way around this issue.

Plus, even if the other switches don't work then at least we have the list of switches which need fixing documented :)

Lowlyslows commented 1 year ago

@charliefoxtwo you got it! Glad we can make this better for others as well.

Lowlyslows commented 1 year ago

@charliefoxtwo See the link below. I can also copy and paste here as well. I did find something interesting going on, see first entry and the red. Let me know what you think and if you have any more questions!

Hope you had a great Christmas!

Electromagnet Switches.docx

Lowlyslows commented 1 year ago

@charliefoxtwo something I noticed between the A10C and the F18 lib files.

A10C has: local defineElectricallyHeldSwitch = BIOS.util.defineElectricallyHeldSwitch which is tied to the SAS switches that I confirmed worked correctly. F18: Doesn't have this local definition in its LUA file, couldn't we add this to the LUA file local definition and replace defineToggleSwitchToggleOnly2("LTD_R_SW", 62, 3002, 441, "Sensor Panel", "LTD/R Switch, ARM/SAFE")

with

defineElectricallyHeldSwitch("LTD_R_SW", 62, 3002, 441, "Sensor Panel", "LTD/R Switch, ARM/SAFE")

I'm probably way out there and should wait on you, just thinking out loud.

Thanks

charliefoxtwo commented 1 year ago

Yeah, I thought that initially here https://github.com/DCSFlightpanels/dcs-bios/issues/134#issuecomment-1363612177

The issue is, that function definition requires two separate argument ids - one for switch up, and one for switch down.

As an example, in the A-10:

defineElectricallyHeldSwitch("SASP_YAW_SAS_L", 38, 3003, 3004, 185, "SAS Panel", "Yaw SAS Left OFF - ENGAGE") 

this has two arguments - 3003 and 3004.

whereas in the F-18:

defineToggleSwitch("LTD_R_SW", 62, 3002, 441, "Sensor Panel", "LTD/R Switch, ARM/SAFE") 

The only argument here is 3002.

We control the definition of defineElectricallyHeldSwitch so we can (and I've attempted to) modify it or create a copy to support this case. But unfortunately, the switch also behaves differently in DCS because it only has the one argument instead of two, so that doesn't work either.

Other electrically-held switches in the F-18 are defined as defineToggleSwitchToggleOnly2, which is why I had you try that to see where that behavior would get us (since it seemed to work for me).

(To clarify, these argument values - e.g. 3002 - are defined by Eagle Dynamics in the modules, and we don't get any control over how much or little they give us)

Lowlyslows commented 1 year ago

@charliefoxtwo ah ok gotcha. Still learning what the lines of code mean. I ended up trying it and the switch didn't act correctly in the game. When I would move the physical switch to ARM it would move the game switch to ARM but when I released the physical switch back to SAFE the game switch would stay at ARM. If I moved the physical switch back to ARM it would then move the game switch back to SAFE. Also wouldn't energize my electromagnet. I set the code back to what was working in the LUA and confirmed it’s functioning.

What do you think about the red lines at the beginning of the doc I attached? Interesting that it functions correctly if I "trick" it.

charliefoxtwo commented 1 year ago

This is some pretty interesting behavior indeed.

Chances are, all the other switches below will function the same way (ON in game first and the ON in real life to be logically turned OFF by the game)

Were there any that you tested that didn't behave this way?

Lowlyslows commented 1 year ago

@charliefoxtwo i can test them as well but they all showed the same behavior before I figured this out so my assumption is yes. Let me know if you need me to confirm

Lowlyslows commented 1 year ago

@charliefoxtwo tested them and they all show the same behavior. I have to turn the switch ON first in the game, then turn my physical switch ON, and then the game will logically turn OFF my physical switch.

charliefoxtwo commented 1 year ago

Got it. It sounds like (just rambling here, this doesn't mean anything) when the switch is toggled on physically, some state value in dcs that gets changed when the switch is clicked in-game isn't being changed - but for some reason, this isn't an issue with the A-10 switches.

I still have no idea why this would be the case, but more information is always helpful. Hopefully we can get to the bottom of this.

Lowlyslows commented 1 year ago

@charliefoxtwo yep. I was thinking on the drive into work that this has something to do with the game switch being commanded ON vs the command to turn ON from DCS-BIOS. Let me know what you would like me to test next!

Lowlyslows commented 1 year ago

@charliefoxtwo more testing: the "Trick" behavior is the same when I use a physical switch to control the FLIR. Meaning, I can set the physical FLIR switch to ON/STBY/OFF without doing it prior with the game switch. Then I do the "trick" with the LTD switch. After that, if I move the physical FLIR switch from ON to STBY it will move the game FLIR switch to STBY and then de-energize the physical electromagnet and move both the LTD game switch and physical switch to SAFE.

Lowlyslows commented 1 year ago

@charliefoxtwo hey! Thinking out loud here. Is there a way to have the code check and see the status of things that would affect the electromagnet from powering on? For example, if the FLIR is in STBY or OFF then the LTD/R electromagnet won't energize.

charliefoxtwo commented 1 year ago

Yeah, but we try not to build in these dependencies for each individual switch. Sometimes game logic can also disable switches without a corresponding switch (e.g. maybe the switch automatically disables after munition impact, regardless of the flir switch - not saying it's the case for this switch, but could be for other switches).

Lowlyslows commented 1 year ago

Yeah, but we try not to build in these dependencies for each individual switch. Sometimes game logic can also disable switches without a corresponding switch (e.g. maybe the switch automatically disables after munition impact, regardless of the flir switch - not saying it's the case for this switch, but could be for other switches).

Makes sense. I have spent the past week trying to figure out why the switch won’t return logically like the A10. I have been trying to stay out of the LUA files as last time I broke DCS and had to reinstall it. I was thinking of comparing the game module LUAs (18 and A10) to see what’s different but haven’t gotten around to it yet.

On a positive note, it’s been fun to code and see stuff work!!🤣

DeadMeatDCS commented 1 year ago

Agree with @charliefoxtwo we shouldn't add switch logic in DCS-BIOS that the already game controls.

@Lowlyslows Mag switches in the F18 are simply implemented in DCS in a different way than for the A10 which is why the old approach to controlling them doesn't work. It's a known problem (well known to some I guess).

If you use DCS-BIOS to send the command to toggle the switch (LTDR, APU, any) you are forcing the switch into the ON position. This is similar to binding it to a keystroke and holding in the key and never releasing it. This can only be overridden by clicking the switch with the mouse in game - or sending the DCS-BIOS command again.

This is why your mag switch is not automatically turning off when you expect it to (e.g.: turning off the battery should turn off the APU, turning off FLIR should turn off LTDR... etc.) It doesn't have anything to do with FLIR or anything. It is simply because you're effectively holding the LTDR switch in by way of DCS-BIOS.

If we stick with the keyboard approach, you'll notice that you can indeed force a switch on (e.g. LTDR), but as soon as you release the key it flips back off IF it should stay off by game logic (e.g. FLIR is off) - or it stays on IF it is allowed.

This is the key to the known, current workaround: use a Arduino Pro Micro (or Leonardo) to emulate a HID keyboard (or joystick) to send a keypress (and key release!!!) that you've bound to the switch in game. You can run DCS-BIOS in the same sketch to monitor the switch position to control your physical magnet. With a bit of logic added you can control and stay in sync with the sim..

DeadMeatDCS commented 1 year ago

To clarify: the LTDR switch is not bugged in DCS or in DCS-BIOS as such. You're seeing a general problem with mag switches in the Hornet compared to the Warthog. Same applies for the other 2-position mag switches.

3-position mags are cursed in their own way to boot - but all can be handled with the HID keyboard/joystick workaround until someone cooks up a better solution.

I'd recommend to close the issue @WarLord211 and @Lowlyslows or maybe better yet rephrase into a general issue around how to deal with mag switches, not just the LTDR. We need a LUA hero here :)

charliefoxtwo commented 1 year ago

Yup, I'm aware of the issue, but the point we've gotten it to (everything works except for when the physical switch is on and game logic toggles it off) suggests to me that there is something we can do to get it working, I'm just not sure what yet.

See https://github.com/DCSFlightpanels/dcs-bios/issues/134#issuecomment-1364421193 which gets us pretty close, just not quite there.

kbastronomics commented 1 year ago

@DeadMeatDCS, I would agree but not. with a closed issue you have little ammunition to take to DCS as you're saying you have a work around. we need this to function correctly. with the Pro-Micro since it supports native HID (which is good) but until DCS-BIOS is fixed we can't use RS-485 which is core to the design of OpenHornet.. so it's a catch 22 for the official OH code. for others rolling their own its an option for sure.

kbastronomics commented 1 year ago

@charliefoxtwo FYI, when I watch both thru status LEDS and on the scope I do see the signal come across to disable the switch magnet via the onApuControlSwChange and onEngineCrankSwChange. I've only played with the APU board at this point as it's the one I've built so far.

Lowlyslows commented 1 year ago

@charliefoxtwo pro micro gets here tomorrow. With the “trick” behavior previously discussed I am sure this will work. Glad others have figured it out already, wish I had ran into open hornet 2 weeks ago🤣

DeadMeatDCS commented 1 year ago

Agree it is not really figured out. Though I maintain that the issue is more general to the F18 and does not only apply to the LTDR switch as the issue suggests. But whatever :)

Here's some input. There was a list of mag switches shared above. Disregarding what may or may not be mag switches the real jet, the following 13 toggles + 1 knob pull are actually implemented as mag switches in DCS, and may call for slight variations in how they are "fixed" due to how they are defined. Test them yourself if you'd like.

ToggleSwitch

ToggleSwitchToggleOnly2

RockerSwitch

MissionComputerSwitch

It could be argued that the gear switch should be included since there's the downlock solenoid keeping it in down postion with WoW... I'll leave that up to someone else to decide :)