Jump to content

Recommended Posts

import time

def generic_check(check_function, duration=2, interval=0):
    start_time = time.time()
    while time.time() < start_time + duration:
        check_function()
        time.sleep(interval)
        print("Hello World")

def check_my_sensors_yo():
    ...

# check sensors for 3 seconds
generic_check(check_my_sensors_yo, 3,1)

Link to comment
Share on other sites

This works it print hello world three times

import time
def generic_check(check_function, duration, interval):
    start_time = time.time()
    while time.time() < start_time + duration:
        check_function()
        time.sleep(interval)
        print("Hello World")

def check_my_sensors_yo():
    ...

# check sensors for 3 seconds
generic_check(check_my_sensors_yo, 3,1)
 

Did not need values inside of the function during defining

Link to comment
Share on other sites

3 minutes ago, Cheif Operations Director said:

This works it print hello world three times

I would not set the default for duration to 0, set it to 0.01 or something. Makes a huge difference on the performance of your code.

Also, out of empathy with the ones helping you, highlight your code and click on the "code" formatting button ('<>') to make it easier to read. Don't forget to select "python" at the right bottom corner of the dialog when doing so.

Link to comment
Share on other sites

It works when its called as well

1 minute ago, Kerbart said:

I would not set the default for duration to 0, set it to 0.01 or something. Makes a huge difference on the performance of your code.

Also, out of empathy with the ones helping you, highlight your code and click on the "code" formatting button ('<>') to make it easier to read. Don't forget to select "python" at the right bottom corner of the dialog when doing so.

 

"""import timedef generic_check(check_function, duration, interval):    start_time = time.time()    while time.time() < start_time + duration:        check_function()        time.sleep(interval)        print("Hello World")def check_my_sensors_yo():    ...# check sensors for 3 secondsgeneric_check(check_my_sensors_yo, 3,1)time.sleep(3)print("break")generic_check(check_my_sensors_yo, 5,1)

 

"""
import time
def generic_check(check_function, duration, interval):
    start_time = time.time()
    while time.time() < start_time + duration:
        check_function()
        time.sleep(interval)
        print("Hello World")

def check_my_sensors_yo():
    ...

# check sensors for 3 seconds
generic_check(check_my_sensors_yo, 3,1)
time.sleep(3)
print("break")
generic_check(check_my_sensors_yo, 5,1)

 

4 minutes ago, Kerbart said:

I would not set the default for duration to 0, set it to 0.01 or something. Makes a huge difference on the performance of your code.

Also, out of empathy with the ones helping you, highlight your code and click on the "code" formatting button ('<>') to make it easier to read. Don't forget to select "python" at the right bottom corner of the dialog when doing so.

import time
def generic_check(check_function, duration, interval):
    start_time = time.time()
    while time.time() < start_time + duration:
        check_function()
        time.sleep(interval)
        print("Hello World")

def check_my_sensors_yo():
    ...

# check sensors for 3 seconds
generic_check(check_my_sensors_yo, 3,0.5)
time.sleep(3)
print("break")
generic_check(check_my_sensors_yo, 5,0.5)

This code prints twice as many words

this is because the duration is three second and it prints a new word every 6 seconds right?

Link to comment
Share on other sites

11 minutes ago, Cheif Operations Director said:

This works it print hello world three times

 

import time
def generic_check(check_function, duration, interval):
    start_time = time.time()
    while time.time() < start_time + duration:
        check_function()
        time.sleep(interval)
        print("Hello World")

def check_my_sensors_yo():
    ...

# check sensors for 3 seconds
generic_check(check_my_sensors_yo, 3,1)
time.sleep(3)
print("break")
generic_check(check_my_sensors_yo, 5,1)

Like this. Not sure how you pasted it. :)

 

Link to comment
Share on other sites

1 minute ago, Kerbart said:

 


import time
def generic_check(check_function, duration, interval):
    start_time = time.time()
    while time.time() < start_time + duration:
        check_function()
        time.sleep(interval)
        print("Hello World")

def check_my_sensors_yo():
    ...

# check sensors for 3 seconds
generic_check(check_my_sensors_yo, 3,1)
time.sleep(3)
print("break")
generic_check(check_my_sensors_yo, 5,1)

Like this. Not sure how you pasted it. :)

 

That's how it looks in my python program

Link to comment
Share on other sites

7 hours ago, Kerbart said:

 


import time
def generic_check(check_function, duration, interval):
    start_time = time.time()
    while time.time() < start_time + duration:
        check_function()
        time.sleep(interval)
        print("Hello World")

def check_my_sensors_yo():
    ...

# check sensors for 3 seconds
generic_check(check_my_sensors_yo, 3,1)
time.sleep(3)
print("break")
generic_check(check_my_sensors_yo, 5,1)

Like this. Not sure how you pasted it. :)

 

Ok I completely debugged the code and made a few simplification edits. 

#Constant Variables

T1 = 2

a = 0

#---------------Import Section
import time
import timeit
#---------------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)

#--------------Functions
#---Math
def add1(x,y):
    Calculator.addition(x,y)

def sub1(x,y):
    Calculator.subtraction(x,y)

def mutl(x,y):
    Calculator.multiplication(x,y)

def div(x,y):
    Calculator.division(x,y)
    

    

def repeatingchecks(duration, interval):
    start_time = time.time()
    while time.time() < start_time + duration:
        time.sleep(interval)
        voltage4(0)
        voltage3(0)
        voltage2(0)
        voltage1(0)

def checksen2():
    pass    

#---Engine Control
    
def RME(A1):
    Temp

#---------------Flight Code Section Secondary

a = 0
def voltage1(b1):
    global c1
    c1 = b1 + 0
    if c1 > a:
        print("Activate Response 1") 
        print("The Value of Voltage +", c1)
        #Open RCS Valve Code
    elif c1==0:
        print("No Excessive Aerodynamic Forces sensor1")
        pass

#---Sensor 2

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


#---Sensor 3

a = 0
def voltage3(b3):
    global c3
    c3 = b3 + 0
    if c3 > a:
        print("Activate Response 3") 
        print("The Value of Voltage +", c3)
        #Open RCS Valve Code
    elif c3==0:
        print("No Excessive Aerodynamic Forces sensor3")
        pass


#---Sensor 4

a = 0
def voltage4(b4):
    global c4
    c4 = b4 + 0
    if c4 > a:
        print("Activate Response 4") 
        print("The Value of Voltage +", c4)
        #Open RCS Valve Code
    elif c4==0:
        print("No Excessive Aerodynamic Forces sensor4")
        pass

#---------------Primary Code Section


repeatingchecks(4,1)
TT = 2
time.sleep (TT)
print("Hello World")

 

I have to say from a theoretical level I still prefer the 

time.clock<b method but since that does not work and your method allows for more control I like it

Thanks it really helped.

:)

Link to comment
Share on other sites

Im curious if anyone has any pointers in terms of functions and the such that may help with me completing the following tasks:

Note: I'm not doing these in this exact order

    1. Simplify the calculator (I think I'm going to just put it all 4  functions and eliminate the class system >insert hammer and sickle< :) ).

    2. Integrate a motor to turn based on me saying activate for x seconds. Perhaps I may need to specify voltage but I doubt it

    3. Integrate the four motors to interrupt their current program and do a course correction. Example in PseudoCode

def activatemotor1(X):
  #ALLOW VOLTAGE TO MOTOR FOR X SECONDS(ASSUMING VOLTAGE IS FIXED RATE)
  
if #TIME IS GREATER THAN >TIME VALUE<# 
  activatemotor1(7)
  #UNLESS 
  a>0
  
 #A IS A VOLTAGE GREATER THAN 1 SIMILAR TO THE SENSOR
#THE IDEA IS THAT RCS WILL DEAL will deal with small gust of wind. HOWEVER IF THE SECOND SENSOR
#triggers (it will be harder to actiavte than the first mechnaically speaking) it will interupt the flight program to counter the stronger gust of wind. EVENTUALLY THIS WILL BE INTEGRATED WITH A GYRO AND GPS BUT THAT IS YEARS OFF.

I'm just looking for pointers or specific areas that I may need to learn throughly in order to do this. One thing I was curious about directly was the libraries and import feature. I've used it based on tutorials and such but what EXACTLY is a library and the import feature. 

Link to comment
Share on other sites

That is not how you want to do this.

You need to rapidly check each sensor, do attitude calculation and find out what to do to correct it. All this has to happen hundreds if not thousands of time per second.

You don't want any predetermined period of corrective action. Each time you go through all the sensors you want to recalculate that corrective action.

Look into PID controller.

Link to comment
Share on other sites

6 hours ago, Cheif Operations Director said:

   1. Simplify the calculator (I think I'm going to just put it all 4  functions and eliminate the class system >insert hammer and sickle< :) ).

I was going to ask why you insist on a Calculator class. Yes, drop it like a hot potato.

why even insist on the arithmetic functions! If you need to multiply x and y, just do x*y. It’s not clear to me why you are wrapping that inside a function.

Link to comment
Share on other sites

3 hours ago, Kerbart said:

I was going to ask why you insist on a Calculator class. Yes, drop it like a hot potato.

why even insist on the arithmetic functions! If you need to multiply x and y, just do x*y. It’s not clear to me why you are wrapping that inside a function.

I'd suspect at least some initial training in Java.  I can't imagine why anyone would build a class to call something built into the language.

Quote

import time
def generic_check(check_function, duration, interval):
    start_time = time.time()
    while time.time() < start_time + duration:
        check_function()
        time.sleep(interval)
        print("Hello World")

def check_my_sensors_yo():
    ...

# check sensors for 3 seconds
generic_check(check_my_sensors_yo, 3,1)
time.sleep(3)
print("break")
generic_check(check_my_sensors_yo, 5,1)

Unfortunately, this looks like something that will lead you to threaded code way too soon.  Of course, so will doing real time rocket control.

Link to comment
Share on other sites

4 hours ago, Kerbart said:

I was going to ask why you insist on a Calculator class. Yes, drop it like a hot potato.

why even insist on the arithmetic functions! If you need to multiply x and y, just do x*y. It’s not clear to me why you are wrapping that inside a function.

Not for use right now but if I ever have to do some mathematics In a special way it is their

Link to comment
Share on other sites

10 hours ago, Shpaget said:

That is not how you want to do this.

You need to rapidly check each sensor, do attitude calculation and find out what to do to correct it. All this has to happen hundreds if not thousands of time per second.

You don't want any predetermined period of corrective action. Each time you go through all the sensors you want to recalculate that corrective action.

Look into PID controller.

I was for now going to do it every 0.25 seconds 

10 hours ago, Shpaget said:

That is not how you want to do this.

You need to rapidly check each sensor, do attitude calculation and find out what to do to correct it. All this has to happen hundreds if not thousands of time per second.

You don't want any predetermined period of corrective action. Each time you go through all the sensors you want to recalculate that corrective action.

Look into PID controller.

This is 100 times a second for 4 seconds

repeatingchecks(4,0.01)

10 thousand times a second for 4 seconds

repeatingchecks(4,0.0001)

 

5 hours ago, Kerbart said:

I was going to ask why you insist on a Calculator class. Yes, drop it like a hot potato.

why even insist on the arithmetic functions! If you need to multiply x and y, just do x*y. It’s not clear to me why you are wrapping that inside a function.

It also cuts down on code

10 hours ago, Shpaget said:

That is not how you want to do this.

You need to rapidly check each sensor, do attitude calculation and find out what to do to correct it. All this has to happen hundreds if not thousands of time per second.

You don't want any predetermined period of corrective action. Each time you go through all the sensors you want to recalculate that corrective action. 

Look into PID controller.

Yes that is a fair point This is more to counter wind basically is Pseudo code

#If wind is true:

    #do stuff

 

When it comes to using GPS's and gyros then I may change this a bit but I will need to get experience first. I doubt I will be using Python by then based on what ive seen.

Link to comment
Share on other sites

  • 2 weeks later...

Hello People I am back again!

I was wondering if their is a way to it an else while statement

#elwh = else + while

def function1(x)
    while x<1:
		print ("Im sick of this")
	elwh x>1:
		print ("This got more fun now")
    elwh x=1:
        print ("Mediocrity is ok")
        
#As you can see this theoretical code is trying to do the same thing as elif but while loop.

 

Link to comment
Share on other sites

7 hours ago, radonek said:

Not sure I get your intent, but it seems to me you just need while True block with bunch of if statements inside.

this is my intention EXCEPT I want to use while loops. This way once it identifies the correct number it while loop instead of doing the action ( in this case printing something) once. 

def function1(x)
	if x>1:
  		print("Boring")
	elif x<1:
    	print("Stil Boring")
    elif x==1:
    	print("So boring")
    else:
    	print("x has transcended the laws of mathematics")

As a note the dumb editor messed with the spaces not me

I will try the last part

 

8 hours ago, radonek said:

Not sure I get your intent, but it seems to me you just need while True block with bunch of if statements inside.

def function1(x):
    while True:
        if x>1:
            print("Hello World1")
        elif x<1:
            print("Hello World2")
        elif x==1:
            print("Hello World3")
        else:
            print("x has broken the laws of mathematics")
            
            
function1(1)

This worked thanks!

Link to comment
Share on other sites

9 hours ago, Cheif Operations Director said:

Hello People I am back again!

I was wondering if their is a way to it an else while statement


#elwh = else + while

def function1(x)
    while x<1:
		print ("Im sick of this")
	elwh x>1:
		print ("This got more fun now")
    elwh x=1:
        print ("Mediocrity is ok")
        
#As you can see this theoretical code is trying to do the same thing as elif but while loop.

 

Sure there is a way. Just put multiple whiles one after the other. If the first while is not satisfied, the code will check if the condition for the second one is. If that one is not satisfied either, the next one is checked.

That being said, do yourself a favor and read (and preferably understand) what I posted earlier.

 

Link to comment
Share on other sites

On ‎6‎/‎16‎/‎2019 at 1:29 PM, Shpaget said:

Sure there is a way. Just put multiple whiles one after the other. If the first while is not satisfied, the code will check if the condition for the second one is. If that one is not satisfied either, the next one is checked.

That being said, do yourself a favor and read (and preferably understand) what I posted earlier.

 

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. 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In other topics if anyone knows how hard it would be to integrate a 3 axis gyroscope. I found some good info online but I was wondering if anyone had any expertise perhaps not from a website that is trying to sell me something... If you need to know who im doing here is the relevant pieces of python code for the flight computer. The gyro code will be edited later to change the parameters to that what is considered on track changes based on time

 

import time

def correctcoursegx(x):
    if x>0:
        print("do stuff")
        
def correctcoursegy(x):
    if x>0:
        print("do stuff")
        
def correctcoursegz(x):
    if x>0:
        print("do stuff")
        
        
        
        
        

#---------------Flight Code Section Secondary


def flightcomputer(gx,gy,gz,c1,c2,c3,c4,duration,interval):
   start_time = time.time()
   while time.time() < start_time + duration:
        time.sleep(interval)
        if 0<gx<361:
            print("off course x ")
            correctcoursegx(111111111111111111111111111111111111111111111)
            if gx>20 or gx>340:
                print("safe destruction x ")
            elif gx<0:
                print("safe destruction x ")
        if 0<gy<361:
            print("off course y ")
            correctcoursegy(111111111111111111111111111111111111111111111)
            if gy>20 or gy>340:
                print("safe destruction y ")
            elif gy<0:
                print("safe destruction y ")
        if 0<gz<361:
            print("off course z ")
            correctcoursegz(111111111111111111111111111111111111111111111)
            if gz>20 or gz>340:
                print("safe destruction z ")
            elif gz<0:
                print("safe destruction z ")
#-------Sensor code            
        if c1 > a:
            print("Activate Response 1") 
            print("The Value of Voltage +", c1)
            #Open RCS Valve Code
        elif c1==0:
            print("No Excessive Aerodynamic Forces sensor1")
            pass
#---Sensor 2
        if c2 > a:
            print("Activate Response 2") 
            print("The Value of Voltage +", c2)
            #Open RCS Valve Code
        elif c2==0:
            print("No Excessive Aerodynamic Forces sensor2")
            pass
 #---Sensor 3
        if c3 > a:
            print("Activate Response 3") 
            print("The Value of Voltage +", c3)
            #Open RCS Valve Code
        elif c3==0:
            print("No Excessive Aerodynamic Forces sensor3")
            pass
#---Sensor 4
        if c4 > a:
            print("Activate Response 4") 
            print("The Value of Voltage +", c4)
            #Open RCS Valve Code
        elif c4==0:
            print("No Excessive Aerodynamic Forces sensor4")
            pass

#---------------Primary Code Section
flightcomputer(1,1,1,1,1,1,1,4,0.10)
TT = 2
time.sleep (TT)
print("Hello World")

 

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

2 hours ago, Cheif Operations Director said:

In other topics if anyone knows how hard it would be to integrate a 3 axis gyroscope.

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.

Link to comment
Share on other sites

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

Ok cool I will try to get this to better interface with it either way I think I managed to find a way to tell the rocket where it is based on its "gimbal" location. It can also change its flight path in flight now as well. Thus I need the gyroscope. Out of curiosity do you know if electronic gyros will becomes unstable like mechanical ones?

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

Unstable as in losing accuracy over time? Of course. It's called drift or noise and it's usually inversely proportional to the cost of the device.

For example, if you take a look at the low cost MPU-6050 datasheet, you may find it is specified as 0,05 °/s. It may not sound a lot but it means that in 10 minutes you can expect it to be 30° off. One of the approaches to dealing with this (other than throwing money at it) is to use two different devices - one that is very precise but not accurate (a gyroscope that drifts), and one that is not so precise but is accurate and doesn't drift (accelerometer). You take accelerometer data to calibrate and cancel the long term drift of the gyro, but use gyro for short term attitude sensing.

Edited by Shpaget
Link to comment
Share on other sites

8 minutes ago, Shpaget said:

Unstable as in losing accuracy over time? Of course. It's called drift or noise and it's usually inversely proportional to the cost of the device. 

For example, if you take a look at the low cost MPU-6050 datasheet, you may find it is specified as 0,05 °/s. It may not sound a lot but it means that in 10 minutes you can expect it to be 30° off. One of the approaches to dealing with this (other than throwing money at it) is to use two different devices - one that is very precise but not accurate (a gyroscope that drifts), and one that is not so precise but is accurate and doesn't drift (accelerometer). You take accelerometer data to calibrate and cancel the long term drift of the gyro, but use gyro for short term attitude sensing.

An interesting Idea, ill see if I can impletment that into the code 

Quick question

Why can I not say

While a=True

    #dostuff

 

Edited by Cheif Operations Director
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...