Jump to content

Recommended Posts

You certainly can. However, depending on what is "a" and what "dostuff" does, it may or may not be a smart thing to do. In a system where there is a need for rapid decision making to correct deviant behavior, such as is the case with a rocket going off the intended course, you want to check the attitude as often as possible, do minimal course correction (because you caught it just a fraction of the moment after it deviated), then check attitude again to see if the correction was adequate (in which case you stop correcting) or it needs another fraction of a moment. You then check again and again.

Getting your code stuck in one while loop opens up the possibility of stuff happening that you intended some other while loop to fix, but the code is stuck in the first one and the second one is left unchecked for too long.

Link to comment
Share on other sites

43 minutes ago, Shpaget said:

You certainly can. However, depending on what is "a" and what "dostuff" does, it may or may not be a smart thing to do. In a system where there is a need for rapid decision making to correct deviant behavior, such as is the case with a rocket going off the intended course, you want to check the attitude as often as possible, do minimal course correction (because you caught it just a fraction of the moment after it deviated), then check attitude again to see if the correction was adequate (in which case you stop correcting) or it needs another fraction of a moment. You then check again and again.

Getting your code stuck in one while loop opens up the possibility of stuff happening that you intended some other while loop to fix, but the code is stuck in the first one and the second one is left unchecked for too long.

I tried that but it said the their is an error






EASLES2=True

def Emergencyabortfunction():
        while True=EASLES2:
            EASLES=1
            if EASLES>0:
                print("Place_Holder_to_detonate_all_stages_and_all_charges")
                time.sleep(4)
                print("Fire_Safety_Pyro_Charges_to_release_parachute_")
                EASLES = False

 

an error on the third line of code 

while True=EASLES2:

                   ^

SyntaxError: invalid syntax

 

 

Sorry for getting back late no notifiation

 

Edited by Cheif Operations Director
Link to comment
Share on other sites

6 hours ago, Cheif Operations Director said:

I was going to time it the thought did escape me in the short term but not long term. Either way I resolved the issue. Thanks! As a note if I connected a few smaller CPUs how could I run the two tasks at the same time. I fixed the problem by putting the ENTIRE flight computer code onto a timed while loop but that seems rather over the top although it works pretty well. 

This is the function that runs it all

def flightcomputer(gx,gy,gz,c1,c2,c3,c4,duration,interval):

it works fine and is very intuitive but it is ridiculous. I would prefer doing the above function as the main computer and then telling another cpu to record flight data etc. 

Why is it ridiculous? It's a pretty common way of running things. The Apollo guidance computer only had a single CPU (plus a couple of separate backup units), if they can fly to the moon on one, you can definitely launch a model rocket on one.

If you want to have multiple CPUs then complexity scales (somewhat non-linearly) with number of CPUs and required interactions between the CPUs. If you just want to have a second core doing telemetry that doesn't talk to the guidance core, that's pretty simple. Once you start making them interact with each other you are in a world of pain.

Link to comment
Share on other sites

4 hours ago, Flibble said:

Why is it ridiculous? It's a pretty common way of running things. The Apollo guidance computer only had a single CPU (plus a couple of separate backup units), if they can fly to the moon on one, you can definitely launch a model rocket on one. 

If you want to have multiple CPUs then complexity scales (somewhat non-linearly) with number of CPUs and required interactions between the CPUs. If you just want to have a second core doing telemetry that doesn't talk to the guidance core, that's pretty simple. Once you start making them interact with each other you are in a world of pain.

Its not ridiculous technically but Its annoying to have to keep track of hundreds of variables on the one function. As for communicating with CPUs why is it a pain?

Link to comment
Share on other sites

6 hours ago, Cheif Operations Director said:

I tried that but it said the their is an error







EASLES2=True

def Emergencyabortfunction():
        while True=EASLES2:
            EASLES=1
            if EASLES>0:
                print("Place_Holder_to_detonate_all_stages_and_all_charges")
                time.sleep(4)
                print("Fire_Safety_Pyro_Charges_to_release_parachute_")
                EASLES = False

 

an error on the third line of code 

while True=EASLES2:

                   ^

SyntaxError: invalid syntax

 

 

Sorry for getting back late no notifiation

 

needed a == sign instead of an  =

Link to comment
Share on other sites

9 hours ago, Shpaget said:

Not particularly. There are plenty of off the shelf solutions using I2C. I'm also sure you won't have much trouble finding relevant libraries.

Reading through the datasheet to integrate a I2C accelerometer can be interesting and may require you to understand low-level programming.  I spent far too long finding out that you needed to send two bytes in "multi-byte mode" instead of sending them one at a time to a similar accelerometer.  Hopefully you can dig up Python libraries/drivers that would work.

1 hour ago, Cheif Operations Director said:

Its not ridiculous technically but Its annoying to have to keep track of hundreds of variables on the one function. As for communicating with CPUs why is it a pain?

You never know exactly when the communication will happen, and if you are sharing memory it has a nasty habit of overwriting half your buffer with different data than the other half (Python only guarentees that each *byte* is correct in such situations.  A 32 bit value can have the most significant bytes from one value and the least significant bytes from another.

If you can structure the overall system in such a way that all communications can be stuck in a queue (google "producer consumer multiprogramming"), nearly all the pain disappears (but it may be slightly worse than one monolithic program, or not depending on the complexity of each unit.

By "stuck in a queue", I mean a single packet (probably a class or something, maybe just a variable) sent to a single point.  That point might be used by more than one process/CPU, but it isn't all that critical *when* it grabs it.  Once this second process is done it will look for a new packet to process.  The system should be able to tolerate more than one item backed up in the queue, especially if you are sticking to an interpreted language like python.

If you can manage to build your system this way, the difference can easily be between "you will never get the bugs out after months of debugging" to "your system is using all the cores/threads in your CPU in less than an hour".  And yes, both situations are from personal experience (although the "in one hour" situation was particularly friendly to parallelism, but it was also my first chance to program a system with multiple CPUs).  And to a certain extant, this is the only real way to use more than one core on a computer in python as the "multiprocessing" module doesn't share memory ("threading" shares memory, but python has a GIL that prevents more than one core/thread operating at once).

Link to comment
Share on other sites

Basically what @wumpus said. As soon as two or more CPUs are competing for the same resource, you're likely to end up with subtle bugs. I have spent a lot of time this week trying to squash a deadlock between two threads (there are several different indepentently locked resources at play so it deadlocks really easily) and it's not much fun.

Link to comment
Share on other sites

1 minute ago, Flibble said:

Basically what @wumpus said. As soon as two or more CPUs are competing for the same resource, you're likely to end up with subtle bugs. I have spent a lot of time this week trying to squash a deadlock between two threads (there are several different indepentently locked resources at play so it deadlocks really easily) and it's not much fun. 



if I define a function where I 

def delegatetocomputer(x)
	#send x to other CPU code
  	# as a note x is the function I want to delgate
    print(y)
    
#(y) is the returned values from the x function

 

10 minutes ago, wumpus said:

Reading through the datasheet to integrate a I2C accelerometer can be interesting and may require you to understand low-level programming.  I spent far too long finding out that you needed to send two bytes in "multi-byte mode" instead of sending them one at a time to a similar accelerometer.  Hopefully you can dig up Python libraries/drivers that would work.



You never know exactly when the communication will happen, and if you are sharing memory it has a nasty habit of overwriting half your buffer with different data than the other half (Python only guarentees that each *byte* is correct in such situations.  A 32 bit value can have the most significant bytes from one value and the least significant bytes from another. 



If you can structure the overall system in such a way that all communications can be stuck in a queue (google "producer consumer multiprogramming"), nearly all the pain disappears (but it may be slightly worse than one monolithic program, or not depending on the complexity of each unit.

By "stuck in a queue", I mean a single packet (probably a class or something, maybe just a variable) sent to a single point.  That point might be used by more than one process/CPU, but it isn't all that critical *when* it grabs it.  Once this second process is done it will look for a new packet to process.  The system should be able to tolerate more than one item backed up in the queue, especially if you are sticking to an interpreted language like python.

If you can manage to build your system this way, the difference can easily be between "you will never get the bugs out after months of debugging" to "your system is using all the cores/threads in your CPU in less than an hour".  And yes, both situations are from personal experience (although the "in one hour" situation was particularly friendly to parallelism, but it was also my first chance to program a system with multiple CPUs).  And to a certain extant, this is the only real way to use more than one core on a computer in python as the "multiprocessing" module doesn't share memory ("threading" shares memory, but python has a GIL that prevents more than one core/thread operating at once). 

I did not understand half of what you said, Im going to look again tomorrow when Im more rested and try looking some stuff up in the mean time

Link to comment
Share on other sites

20 hours ago, Cheif Operations Director said:

if I define a function where I 


def delegatetocomputer(x)
	#send x to other CPU code
  	# as a note x is the function I want to delgate
    print(y)
    
#(y) is the returned values from the x function

 

I did not understand half of what you said, Im going to look again tomorrow when Im more rested and try looking some stuff up in the mean time

If you simply wait until the function returns a value, it makes little sense to delegate the work (you might as well have the original processor do the work).  The whole issue comes up when the initial processor keeps going and the other processor completes the work at some unpredictable time, often  causing unforeseen difficulties.

I suspect that my wall of text will only make sense once you look into the various options of running on more than one CPU, especially with python.  If you plan on multiple CPUs on a rocket, you might want to go with a raspberry pi or similar (multiple cores, each capable of running python).

Link to comment
Share on other sites

I honestly don't see the need for multiple CPUs. I'd wager a bag of gummy bears that the task at hand can be adequatly performed by a single microcontroller. 

Keep it simple. You're struggling with even basic programing stuff. Get that sorted out first. You don't need additional hurdles (nor the benefit of more computing power).

Link to comment
Share on other sites

4 hours ago, wumpus said:

If you simply wait until the function returns a value, it makes little sense to delegate the work (you might as well have the original processor do the work).  The whole issue comes up when the initial processor keeps going and the other processor completes the work at some unpredictable time, often  causing unforeseen difficulties. 

I suspect that my wall of text will only make sense once you look into the various options of running on more than one CPU, especially with python.  If you plan on multiple CPUs on a rocket, you might want to go with a raspberry pi or similar (multiple cores, each capable of running python).

I could return the value first and let the function carry on?

 

2 minutes ago, Shpaget said:

I honestly don't see the need for multiple CPUs. I'd wager a bag of gummy bears that the task at hand can be adequatly performed by a single microcontroller. 

Keep it simple. You're struggling with even basic programing stuff. Get that sorted out first. You don't need additional hurdles (nor the benefit of more computing power).

Fair enough

Link to comment
Share on other sites

6 hours ago, Cheif Operations Director said:

I could return the value first and let the function carry on?

Tested that it did not work, oh well as @Shpaget pointed out I really do not need a new CPU


x=2

def secondcpu(x):
    while x==2:
        return x and thirdcpu()
    
        
def firstcpu(x):
    secondcpu(x)
    print(x)
    
def thirdcpu():
    while x==2:
        print("Blah")
    
    
firstcpu(x)

 

 

Edited by Cheif Operations Director
Link to comment
Share on other sites

Im trying to multiply all the variables in a class by 3 instead of individually multiplying them. I have a reason for this. Any ideas?

global var1
global var2
global var3

class variable:
    def var1():
        var1==1
    def var2():
        var2==2
    def var3():
        var3==3

variable * 3
print(var1,var2,var3)

 

Edited by Cheif Operations Director
Link to comment
Share on other sites

How can I assign TWO values to a single variable? I need to do this to convince the flight computer of its position

I tried this

T=10000
x2=0
if T>x2:
    1==300
    2==600
    3==900

x=300
if x==301 or x==1:
    print("hello")

 

and this

T=10000
x2=0
if T>x2:
    a=1 and a =300
    b=2 and b =600
    c=3 and c =900
    
    
    
if a==301 or a==1:
    print("hello")

 

Edited by Cheif Operations Director
Link to comment
Share on other sites

7 hours ago, Shpaget said:

You can't. A variable is a number. A number can not be two different numbers.

Also, I don't think this:


if T>x2:
    1==300
    2==600
    3==900

will do what you think it will do.

There are ways to store multiple numbers, but they all involve grouping multiple variables.  Python loves lists, but you could just as easily use a class for multiple variables. 

Variable names can't be numbers.  I'm guessing you tried to type 1=300 and it failed (the compiler wouldn't let you do it).  1==300 means something completely different.  Even

Quote

  if T>x2:
    var1==300
    var2==600
    var3==900

is unlikely to do what you are trying to do (and they are variables).

Link to comment
Share on other sites

3 hours ago, wumpus said:

they all involve grouping multiple variables

Yes, one variable for each stored value. Arrays, lists, or just var1a, var1b, var2a, var2b, they can all be employed for storing multiple values, but that's not what OP asked.

Link to comment
Share on other sites

First... I checked multiple times and did not get notifications weird.
Second @Shpaget I need to convince my code of its location. If the Gyros angle number (gx) is greater than one tolerance or less than another it will detonate. This way the rocket will not flyoff and just disintegrate safely in the air. If the rocket goes straight up at 0/360 degrees I set the tolerances so if it is less than 340 and greater than 20 it will detonate. (As a note I just realized I coded that wrong although this does not affect the problem being discussed here) when I turn the rocket by 30* on one axis (pitch over) I need to change these tolerances. If I did this the code would respond to 370* (as it is 10 degrees outside the 20 degree tolerance) but not 10* I do not want to recode each and every turn program because in the end I may have hundreds of then and changing all of those is a joke of epic proportions. I would rather amend a single variable as I have done. 

 if FTminx<gx<FTmaxx:
            print("On course no adjustment needed (x)")
        elif FTminx>gx:
            print("Course Correction to the Negative (x)")
            coursecorrectionxn()
        elif FTmaxx<gx:
            print("Course Correction to the Positive (x)")
            coursecorrectionxp()
#----THE CODE BELOW THIS
        if gx>minangletolerance or gx>maxangletolerance:
            print("safe destruction x ")
            Emergencyabortfunction()
#---- THE CODE ABOVE THIS            
        elif gx<0:
            print("safe destruction x ")
            Emergencyabortfunction()

 

13 hours ago, wumpus said:

There are ways to store multiple numbers, but they all involve grouping multiple variables.  Python loves lists, but you could just as easily use a class for multiple variables. 

Variable names can't be numbers.  I'm guessing you tried to type 1=300 and it failed (the compiler wouldn't let you do it).  1==300 means something completely different.  Even

is unlikely to do what you are trying to do (and they are variables).

I tried that and it did not work correctly

T=10000
x2=0
if T>x2:
    a = [1, 300]
    
if a==300:
     print("hello")
if a==1:
    print("hello")

 

Link to comment
Share on other sites

Ok... As a matter of practicality I have this working the way I want to too. My original problem with the two numbers equaling the same thing has not been solved but I managed to get a work around using True and False statements. If your are interested Basically I recreated the function but with different parameters to determine if the rocket is off course. I turn on and off the two way of figuring this out with if statements that judge if a Variable is true or not. Took longer than I would want but whatever. Thanks @Shpaget for the info about the variables. @Kerbart Thanks for the time clock thing that was incredible useful. 

Spoiler

#Initally/When the code starts running
FLIGHTVAR=0
FLIGHTVARII=1
EASLES2=True
INFLIGHTVAR=True
INFLIGHTVARII=False

 #---
        if INFLIGHTVARII==True:
            if FLIGHTVAR>FLIGHTVARII:
                if gz<minangletolerance or gz>maxangletolerance:
                    print("safe destruction z ")
                    Emergencyabortfunction()
       #---
        if INFLIGHTVAR==True:
            if gz>minangletolerance and gz<maxangletolerance:
                print("safe destruction z ")
                Emergencyabortfunction()
        elif gz<0:
            print("safe destruction z ")
            Emergencyabortfunction()

#After you are ready to change the "rules"
FLIGHTVAR=1
FLIGHTVARII=0
INFLIGHTVAR=False
INFLIGHTVARII=True

 

 

Edited by Cheif Operations Director
Link to comment
Share on other sites

19 hours ago, Cheif Operations Director said:

Ok... As a matter of practicality I have this working the way I want to too.

Progress! That’s what we do it for

 

19 hours ago, Cheif Operations Director said:

My original problem with the two numbers equaling the same thing has not been solved but I managed to get a work around using True and False statements. If your are interested Basically I recreated the function but with different parameters to determine if the rocket is off course

The one thing I would recommend is using more descriptive names for “inflight;” what’s the difference between the four of them? I don’t intend you to answer that question here, but rather inside the program; if they represent various flight stages consider “stage_liftoff” “stage_roll” etc.

19 hours ago, Cheif Operations Director said:

 

 

A few more generic suggestions:

  • An if statement checks for an expression that yields a boolean value. So instead of writing “if var == True:” you can simply type “if var:” which is preferred as there is less code to read.
  • While interchanging True and False with 1 and 0 perfectly works I’d stick to boolean values for consistency. It’s one of those things which makes your code easier to understand. Future you will thank you
  • common practice in python is to separate words in names with underscores, and use all lowercase. Since what you’re calling has to be a function, make it emergency_abort() instead of Emergencyabortfunction()

While most of the experienced coders would likely do it different, finding your own way is important in learning how to code. Congratulations on reaching your (preliminary) goal and keep going at it!

Link to comment
Share on other sites

4 minutes ago, Kerbart said:

Progress! That’s what we do it for

 

The one thing I would recommend is using more descriptive names for “inflight;” what’s the difference between the four of them? I don’t intend you to answer that question here, but rather inside the program; if they represent various flight stages consider “stage_liftoff” “stage_roll” etc.



A few more generic suggestions:

  • An if statement checks for an expression that yields a boolean value. So instead of writing “if var == True:” you can simply type “if var:” which is preferred as there is less code to read.
  • While interchanging True and False with 1 and 0 perfectly works I’d stick to boolean values for consistency. It’s one of those things which makes your code easier to understand. Future you will thank you
  • common practice in python is to separate words in names with underscores, and use all lowercase. Since what you’re calling has to be a function, make it emergency_abort() instead of Emergencyabortfunction()

While most of the experienced coders would likely do it different, finding your own way is important in learning how to code. Congratulations on reaching your (preliminary) goal and keep going at it!

1. Indeed

2. I did that for simplification. Their was no benefit to doing alternates atleast not yet. If I ever have to change it it only requires a few more lines of code

3.a. How will it know if I want it to be if var==True of var==False

3.b. I could most likely due that I just wanted to make it easier to change although in hindsight it will most likely be the same anyways.

3.c. I will make the underscore change but I will not remove the Capital as I intend to have a "soft" abort function by the same name that will not destroy the whole rocket but just the part I need it too. ( I can not do this until the rocket is built because I need to know more about the rocket itself)

4. Thanks. As a note do you have any experience with gyroscopes? If you do I can directly import the value of the gyroscope into a variable. This goes into a bigger question which is how do I interface pins and a CPU etc with this code?

Link to comment
Share on other sites

40 minutes ago, Cheif Operations Director said:

. I will make the underscore change but I will not remove the Capital as I intend to have a "soft" abort function by the same name that will not destroy the whole rocket but just the part I need it too. ( I can not do this until the rocket is built because I need to know more about the rocket itself)

That's a recipe for disaster.

Link to comment
Share on other sites

Having near exactly the same names (as in with the only difference being the upper or lower case) will lead to confusion and usage of the wrong one, which will result in unexpected behavior that can be very difficult to debug.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...