Jump to content

Recommended Posts

2 minutes ago, IncongruousGoat said:

Wait. What do you want the result of calling this function to be? Right now, you've got it doing two things: You set the value of a global variable 'c' equal to the value of the input 'b', and you return the value of the input 'b'. 

The 'return' keyword doesn't do anything with moving variables around. All it does is give the function call a value that you can then assign to stuff. So, for example, you could say something like: 'd = voltage(100)', and the result would be a new variable 'd' with the value 100. If you just call the function without assigning its return to something, the return value is still calculated, but the return value is thrown away (as in, the compiler forgets what it is) immediately, since you didn't tell it to do anything with it.

Here is the rundown: I want this function to only be activated when a value is inserted into it for example (1). I want this that is inserted (b) = a new value (c). Then I want the code to evaluate if c is larger than a (0). If it is do something if it is not do not do something. this is supposed to happen a few times a second. I making this for a guidance system for a model rocket.

Basically if a sensor is triggered is causes the b value (voltage) to be >0. Thus this code will then activate a new system the print line is temporary. it just conveys the idea

If the sensor is not triggered then no response

a = 0
def voltage(b):
    global c
    c= b+0
voltage(b)
if c>a:
    print("Activate Response") 
    print("The Value of Voltage-", c)

I think this may have resolved the issues. On the fifth line you will need to replace the b value with a number.

Link to comment
Share on other sites

Just now, Cheif Operations Director said:

Here is the rundown: I want this function to only be activated when a value is inserted into it for example (1). I want this that is inserted (b) = a new value (c). Then I want the code to evaluate if c is larger than a (0). If it is do something if it is not do not do something. this is supposed to happen a few times a second. I making this for a guidance system for a model rocket.

You're making this for the guidance system for a model rocket? What hardware are you planning to use to control this model rocket? If you've just got a radio transceiver on it and are doing all the computation on a groundstation, you'll probably be fine, but if you want to mount the guidance computer in the model rocket itself then Python is not the right choice of language. There do exist single-board computers that'll fit in a model rocket (think a Raspberry Pi but smaller), but they're expensive and a bit overkill. What you probably want is an Arduino, or something a lot like it. Those definitely do come small enough to fit in a model rocket, but they can only be programmed in C/C++. They won't and, in fact, can't run Python.

Link to comment
Share on other sites

Just now, IncongruousGoat said:

You're making this for the guidance system for a model rocket? What hardware are you planning to use to control this model rocket? If you've just got a radio transceiver on it and are doing all the computation on a groundstation, you'll probably be fine, but if you want to mount the guidance computer in the model rocket itself then Python is not the right choice of language. There do exist single-board computers that'll fit in a model rocket (think a Raspberry Pi but smaller), but they're expensive and a bit overkill. What you probably want is an Arduino, or something a lot like it. Those definitely do come small enough to fit in a model rocket, but they can only be programmed in C/C++. They won't and, in fact, can't run Python. 

Why would Python not be right for mounting it inside of the rocket. Others have mentioned Arduinos and Im not against it but based on what ive seen Python seems to be the better language overall. The hardware I have not really determined yet I wanted to get the code working in some form so I could get the right stuff instead of buying things to realize they will not work.

Link to comment
Share on other sites

12 minutes ago, Cheif Operations Director said:

I want this function to only be activated when a value is inserted into it for example (1). I want this that is inserted (b) = a new value (c). Then I want the code to evaluate if c is larger than a (0). If it is do something if it is not do not do something.

In that case, you probably want the 'if' statement to be inside the function:

a = 0

def voltage(b):
    global c
    c = b + 0
    if c > a:
        print("Activate Response") 
        print("The Value of Voltage-", c)
        
voltage(-100)
voltage(100)

 

Link to comment
Share on other sites

5 minutes ago, HebaruSan said:

In that case, you probably want the 'if' statement to be inside the function:


a = 0

def voltage(b):
    global c
    c = b + 0
    if c > a:
        print("Activate Response") 
        print("The Value of Voltage-", c)
        
voltage(-100)
voltage(100)

 

It works thanks and cool. Also that quote from Slavoj im not sure he said it do you have a citation? :wink:

Link to comment
Share on other sites

1 minute ago, Cheif Operations Director said:

The hardware I have not really determined yet I wanted to get the code working in some form so I could get the right stuff instead of buying things to realize they will not work.

You can't write the code without knowing what hardware you're using. Well, you can write some code, but you're going to need to adapt it to the particular hardware you're using, and in some cases "adapting" the code means re-writing all of it. Any piece of hardware you find out there will have at least some programming languages that it doesn't support, and about the only language that's guaranteed to be supported everywhere is C. It might be that whatever hardware you're using supports Python (heck, this might be one of the criteria you use when selecting hardware), but it also might not. Whatever the case, it's best to know what hardware you're using before you start writing any software for it. You don't have to buy it right away (though you might need to for testing), but you should at least know what it is.

Link to comment
Share on other sites

10 minutes ago, IncongruousGoat said:

You can't write the code without knowing what hardware you're using. Well, you can write some code, but you're going to need to adapt it to the particular hardware you're using, and in some cases "adapting" the code means re-writing all of it. Any piece of hardware you find out there will have at least some programming languages that it doesn't support, and about the only language that's guaranteed to be supported everywhere is C. It might be that whatever hardware you're using supports Python (heck, this might be one of the criteria you use when selecting hardware), but it also might not. Whatever the case, it's best to know what hardware you're using before you start writing any software for it. You don't have to buy it right away (though you might need to for testing), but you should at least know what it is. 

I suppose I will start looking at hardware. Is their any reason why I could not use Python to make a guidance system/flight computer from a technical perspective?

Link to comment
Share on other sites

Just now, Cheif Operations Director said:

I suppose I will start looking at hardware. Is their any reason why I could not use Python to make a guidance system/flight computer from a technical perspective?

Other than hardware not supporting it, no, but there's a couple reasons why Python might not be the best choice for a flight computer:

1. It's very slow. As in, a program written in Python will run slower than an equivalent program written in, say, C, or Java. Since flight computers typically need to be quite responsive, you might not want to use a slower language.

2. It's very abstracted. That is to say, the data types and constructs you use in Python are mostly unrelated to what the underlying computer is actually doing. This makes interacting with hardware in Python a frustrating process. It's possible, but it requires use of weird and obscure bits of the Python standard library.

Of course, I speak as someone who thinks Python is largely worthless as a language, so take what I say with a grain of salt.

Link to comment
Share on other sites

1 minute ago, IncongruousGoat said:

Other than hardware not supporting it, no, but there's a couple reasons why Python might not be the best choice for a flight computer:

1. It's very slow. As in, a program written in Python will run slower than an equivalent program written in, say, C, or Java. Since flight computers typically need to be quite responsive, you might not want to use a slower language. 

2. It's very abstracted. That is to say, the data types and constructs you use in Python are mostly unrelated to what the underlying computer is actually doing. This makes interacting with hardware in Python a frustrating process. It's possible, but it requires use of weird and obscure bits of the Python standard library.

Of course, I speak as someone who thinks Python is largely worthless as a language, so take what I say with a grain of salt.

Ok I will. I seem to be getting a similar consensus so I will look into it more. 

Link to comment
Share on other sites

1 minute ago, Bill Phil said:

Arduino could work well, though that depends on a few factors (size of rocket and others).

Well, except then @Cheif Operations Director is stuck writing in C. Now, I like C much more than the next guy, but it's not exactly great as a first language for someone who isn't really into computer science.

Link to comment
Share on other sites

13 minutes ago, Bill Phil said:

Arduino could work well, though that depends on a few factors (size of rocket and others).

Its not going to be an Estes rocket but it will not be a high powered rocket either.

 

https://arduino.stackexchange.com/questions/105/programming-an-arduino-using-python-rather-than-c-c

I found this online and was wondering as to everyone's opinion

This is all the code for the guidance system as of right now. 

---------------Import Section
import time
#---------------Class Section
class Calculator:
    def addition (x,y):
        add = x+y
        print(add)
  
    def subtraction (x,y):
        sub = x-y
        print(sub)
   
    def multiplication (x,y):
        mult = x*y
        print(mult)
   
    def division (x,y):
        div = x/y
        print(div)
#---------------Flight Code Section Secondary
a = 0
def voltage1(bb1):
    global cc1
    cc1= bb1+0
voltage1(0)
if cc1>0:
    print("Activate Response sensor1") 
    print("The Value of Voltage sensor1 +", cc1)
elif cc1==0:
    print("No Excessive Aerodynamic Forces sensor1")
#---Sensor 2
a = 0
def voltage2(bb2):
    global cc2
    cc2 = bb2+0
voltage2(0)
if cc2>0:
    print("Activate Response sensor2") 
    print("The Value of Voltage sensor2 +", cc2)
elif cc2==0:
    print("No Excessive Aerodynamic Forces sensor2")
#---Sensor 3
a = 0
def voltage3(bb3):
    global cc3
    cc3 = bb3+0
voltage3(0)
if cc3>0:
    print("Activate Response sensor3") 
    print("The Value of Voltage sensor3 +", cc3)
elif cc3==0:
    print("No Excessive Aerodynamic Forces sensor3")
#---Sensor 4
a = 0
def voltage4(bb4):
    global cc4
    cc4 = bb4+0
voltage4(0)
if cc4>0:
    print("Activate Response sensor4") 
    print("The Value of Voltage sensor4 +", cc4)
elif cc4==0:
    print("No Excessive Aerodynamic Forces sensor4")
   
#---------------Primary Code Section
TT = 10
time.sleep (TT)
print("Hello World")

 

 

 

 

11 minutes ago, IncongruousGoat said:

Well, except then @Cheif Operations Director is stuck writing in C. Now, I like C much more than the next guy, but it's not exactly great as a first language for someone who isn't really into computer science.

Also what I hear which is why I took Python

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

24 minutes ago, IncongruousGoat said:

in some cases "adapting" the code means re-writing all of it.

This may be a blessing in disguise for a first project. Learn the basics of programming, get the project prototyped in Python, and then once it does roughly what you want it to, go through the pain of learning C/C++ (or whatever the optimal target language is) to create the "real" version of the project.

(I've been attempting a little bit of modding for Satisfactory, and boy howdy are C++ linker errors not fun. I can't imagine having to deal with them within my first 100 hours of programming.)

Link to comment
Share on other sites

9 minutes ago, Cheif Operations Director said:

https://arduino.stackexchange.com/questions/105/programming-an-arduino-using-python-rather-than-c-c

 

I found this online and was wondering as to everyone's opinion

I took a look at that earlier. Here's my opinion on it, answer by answer:

1. This answer seems to cover it pretty well. Arduinos are really tiny. Their clock speed is measured in megahertz and the amount of RAM the have available is measured in kilobytes. You're going to have a really hard time fitting the Python interpeter into that. Compiling Python to something that can run on an Arduino might be possible, from a technical perspective, but it's something I (as an experienced programmer) would be apprehensive to try to get working. For your purposes, it's not possible.

2. This only works if the Arduino is plugged into a computer. Not very useful for a model rocket.

3. This looks interesting, but a bit of digging reveals a few problems. It requires 55 KB of system memory, which means a larger or more expensive Arduino. However, what really kills it is that (according to the people who made it) you still need to know C and a good amount about microcontrollers to get it set up in the first place.

4. This is just a manual version of 2

5. Unfortunately, the chip that answer talks about has been discontinued.

6. Not actually an answer.

7. Seems to just be 2 and 4 again.

8. This guy doesn't have any idea what he's talking about.

Link to comment
Share on other sites

2 minutes ago, IncongruousGoat said:

I took a look at that earlier. Here's my opinion on it, answer by answer:

1. This answer seems to cover it pretty well. Arduinos are really tiny. Their clock speed is measured in megahertz and the amount of RAM the have available is measured in kilobytes. You're going to have a really hard time fitting the Python interpeter into that. Compiling Python to something that can run on an Arduino might be possible, from a technical perspective, but it's something I (as an experienced programmer) would be apprehensive to try to get working. For your purposes, it's not possible.

2. This only works if the Arduino is plugged into a computer. Not very useful for a model rocket.

3. This looks interesting, but a bit of digging reveals a few problems. It requires 55 KB of system memory, which means a larger or more expensive Arduino. However, what really kills it is that (according to the people who made it) you still need to know C and a good amount about microcontrollers to get it set up in the first place.

4. This is just a manual version of 2

5. Unfortunately, the chip that answer talks about has been discontinued.

6. Not actually an answer.

7. Seems to just be 2 and 4 again.

8. This guy doesn't have any idea what he's talking about.

Lol on 8

4 minutes ago, HebaruSan said:

This may be a blessing in disguise for a first project. Learn the basics of programming, get the project prototyped in Python, and then once it does roughly what you want it to, go through the pain of learning C/C++ (or whatever the optimal target language is) to create the "real" version of the project.

(I've been attempting a little bit of modding for Satisfactory, and boy howdy are C++ linker errors not fun. I can't imagine having to deal with them within my first 100 hours of programming.)

I suppose so. I want to eventually know C but as of noon yesterday I knew literally nothing when it came to this type of coding this is my first full day of it and I do not want to undertake a more advanced language on my first try.

Link to comment
Share on other sites

4 minutes ago, HebaruSan said:

(I've been attempting a little bit of modding for Satisfactory, and boy howdy are C++ linker errors not fun. I can't imagine having to deal with them within my first 100 hours of programming.)

You think linker errors are bad? Try template errors sometime. I just love it when my compiler decides that what I really need is a single error message that's 500 lines long.

But, for Arduino, you avoid both linker errors and template errors. Linker errors because the Arduino IDE handles all the stuff that could cause linker errors for you behind the scenes, and template errors because... well... if you're using templates in code for a microcontroller, something has gone horribly wrong.

Link to comment
Share on other sites

Do any of you see any areas in the flight computer above that may need improvement for what I have now.

Again note that once I have the full code written out to print a text I will add sections so it can interface with the hardware. The last section basically tells the rocket from the moment of launch to wait 10 second and then begin a gravity turn. 

I inserted the calculator so any calculations when I develop this further are able to be done. 

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

22 minutes ago, IncongruousGoat said:

Well, except then @Cheif Operations Director is stuck writing in C. Now, I like C much more than the next guy, but it's not exactly great as a first language for someone who isn't really into computer science.

Yeah. But it isn't that bad.

My first language was Fortran though so I guess C was a little easier...

Link to comment
Share on other sites

8 minutes ago, Cheif Operations Director said:

Do any of you see any areas in the flight computer above that may need improvement for what I have now.

You've taken the 'if' statements out of the functions again. Code that reacts to the sensor values belongs in the function that handles the sensor.

12 minutes ago, Cheif Operations Director said:

The last section basically tells the rocket from the moment of launch to wait 10 second and then begin a gravity turn. 

Where "gravity turn" is defined as "print hello world"? ;)

Link to comment
Share on other sites

 

 

2 minutes ago, HebaruSan said:

You've taken the 'if' statements out of the functions again. Code that reacts to the sensor values belongs in the function that handles the sensor.

Where "gravity turn" is defined as "print hello world"? ;)

if cc1>0: uses an if statement im not sure what you mean.

Yes most (not all) of those prints will be replaced when I actually know how to interface the hardware. 

The comedy of that just occurred on me however

How does it work differently if I change it to the other way

Link to comment
Share on other sites

err your right

#---------------Import Section
import time
#---------------Class Section
class Calculator:
    def addition (x,y):
        add = x+y
        print(add)
  
    def subtraction (x,y):
        sub = x-y
        print(sub)
   
    def multiplication (x,y):
        mult = x*y
        print(mult)
   
    def division (x,y):
        div = x/y
        print(div)
#---------------Flight Code Section Secondary
a = 0

def voltage1(b1):
    global c1
    c1 = b1 + 0
    if c1 > a:
        print("Activate Response") 
        print("The Value of Voltage +", c1)
    elif c1==0:
        print("No Excessive Aerodynamic Forces sensor2")
        
voltage1(0)

#---Sensor 2
a = 0

def voltage2(b2):
    global c2
    c2 = b2 + 0
    if c2 > a:
        print("Activate Response") 
        print("The Value of Voltage +", c2)
    elif c2==0:
        print("No Excessive Aerodynamic Forces sensor2")
        
voltage2(0)

#---Sensor 3
a = 0

def voltage3(b3):
    global c3
    c3 = b3 + 0
    if c3 > a:
        print("Activate Response") 
        print("The Value of Voltage +", c3)
    elif c3==0:
        print("No Excessive Aerodynamic Forces sensor2")
        
voltage3(0)

#---Sensor 4
a = 0

def voltage4(b4):
    global c4
    c4 = b4 + 0
    if c4 > a:
        print("Activate Response") 
        print("The Value of Voltage +", c4)
    elif c4==0:
        print("No Excessive Aerodynamic Forces sensor2")
        
voltage4(0)

   
#---------------Primary Code Section
TT = 10
time.sleep (TT)
print("Hello World")

9 minutes ago, HebaruSan said:

When the sensor calls the voltage1 function later, the 'if' statement will not execute the way you have it.

I think I managed to adapt it well enough

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...