Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a return code with wait_packet to trigger code #1794

Open
gutelfuldead opened this issue Oct 29, 2022 · 1 comment
Open

Using a return code with wait_packet to trigger code #1794

gutelfuldead opened this issue Oct 29, 2022 · 1 comment

Comments

@gutelfuldead
Copy link

gutelfuldead commented Oct 29, 2022

I have a script that is continually running logging telemetry. I have a specific telemetry message that indicates an error that can be sent randomly. I would like to check for this message and if it appears print it, otherwise the script keeps looping printing out the other telemetry. Doesn't seem like I can do this though. It may just be my limited Ruby blocking me.

Example code,

loop do
  # do a bunch of telemetry wait_check_packet commands here
  # at the end see if we got any error messages as well
   begin                                                                                                                                                                                                       
        if (wait_packet("#{target}", 'ERROR_MSG', 1, 5))                                                                                                                                             
            cmd = tlm("#{target} ERROR_MSG CMD")                                                                                                                                                           
            line = tlm("#{target} ERROR_MSG LINE")                                                                                                                                                         
            err = tlm("#{target} ERROR_MSG ERROR")                                                                                                                                                         
            puts "!!! ERROR_MSG Received !!!"                                                                                                                                                              
            puts "CMD : #{cmd} ... LINE : #{line} ... ERROR : #{err}"                                                                                                                                              
        end        
end

If one message was ever received in the past this will just continue to print out the contents of that message every iteration; which I understand is why wait_check_packet is typically used but doesn't seem appropriate in this use case. I tried placing a not in front of wait_packet and it just always ignores it.

EDIT : Forgot to mention I am locked into using a legacy version of COSMOS, v4.3.0

@AbdelAzizMohamedMousa
Copy link

It looks like the issue you're facing is that once you receive an error message, it continues to get printed in every iteration of the loop. One way to solve this issue would be to introduce a flag that gets set to true when an error message is received and gets reset to false once it has been printed.

Here's an example code that uses a received_error flag to accomplish this:
`received_error = false

loop do

do a bunch of telemetry wait_check_packet commands here

at the end see if we got any error messages as well

if wait_packet("#{target}", 'ERROR_MSG', 1, 5)
cmd = tlm("#{target} ERROR_MSG CMD")
line = tlm("#{target} ERROR_MSG LINE")
err = tlm("#{target} ERROR_MSG ERROR")
if !received_error
puts "!!! ERROR_MSG Received !!!"
puts "CMD : #{cmd} ... LINE : #{line} ... ERROR : #{err}"
received_error = true
end
else
received_error = false
end
end
`
In this code, we initialize received_error to false before entering the loop. When an error message is received, we check if received_error is false. If it is, we print the error message and set received_error to true. If it is true, we skip printing the error message. In each iteration, we also check if an error message was not received (wait_packet returns false) and reset received_error to false. This way, we only print the error message once when it first appears and avoid printing it in every iteration of the loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants