Jump to content

Recommended Posts

First for those who have remembered my old threads on coding I have decided to bite the proverbial bullet and learn python soon. I wanted to ask a few questions.

First I have been informed by a mostly reliable source that Python can interface with hardware such as motors and electric "stuff" in general. This is true right?

Secondly I have no intention of doing this anytime in the next year but is Python capable of making an AI of sorts.

I ask all of this because I want to make a model rocket that has a full guidance system as well as staging system ect and I want to make sure before I put a bunch of time into learning this code that is it worth my time and is what Im looking for. Is this true? Finally are they any pointers that people can suggest to learning this language if it is really what im looking for. 

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

Python should be capable of all that with the right libraries.

I would recommend something else though if that's what your doing.

I don't know exactly what you need but an Arduino might be able to do it. I've actually integrated an Arduino with a 6-axis gyro/accelerometer before, so it has the capability for rudimentary guidance and maybe other things as well.

Python is cool because it's interpreted - meaning no compiling is necessary. But this also gives it a few drawbacks when it comes to performance, though this has become less relevant as hardware has improved.

Link to comment
Share on other sites

2 minutes ago, Bill Phil said:

Python should be capable of all that with the right libraries.

I would recommend something else though if that's what your doing.

I don't know exactly what you need but an Arduino might be able to do it. I've actually integrated an Arduino with a 6-axis gyro/accelerometer before, so it has the capability for rudimentary guidance and maybe other things as well.

Python is cool because it's interpreted - meaning no compiling is necessary. But this also gives it a few drawbacks when it comes to performance, though this has become less relevant as hardware has improved.

Ive heard of the Arduino stuff and know it is good. What do you mean of by the right libraries?

Can Arduino use Python?

Link to comment
Share on other sites

1 hour ago, Cheif Operations Director said:

Ive heard of the Arduino stuff and know it is good. What do you mean of by the right libraries?

Can Arduino use Python?

I’m not exactly sure about it but libraries are essentially extra functions for a language. Like a graphics library so the language’s interpreter or compiler or what have you can access a computer’s graphical output, or the sound system, or take in certain inputs like a joystick or gamepad. 

Arduino has its own language.

Another library example would be when I used the libraries for the accelerometer. It turned out there was a library with a faster function, so I switched which one I used in my program. 

Libraries generally carry the stuff for interfacing with servos and motors and other hardware. If it’s a language designed for that (RobotC) then it doesn’t need extra libraries, but Python certainly isn’t that.

Link to comment
Share on other sites

44 minutes ago, Bill Phil said:

Arduino has its own language.

Actually Arduino uses C/C++, but has a pre-processor that auto-generates some of the things you normally need to do by hand (i.e. generating header files). It will gladly accept ordinary C/C++ in addition to the simplified version that only works with this pre-processor.

There are a couple of projects that use Arduino's in quadcopters, which means that there is code out there for interfacing with sensors and making real-time adjustments to maintain stable flight. Unfortunately I checked my old links and they seem to be defunct. Having said that that I'm sure there are a few projects covered on Hackaday that would help you out.

One thing I'll point out is before you do this is check all relevant laws (nation, state/province, city). Depending on where you live, building a fully guided rocket may put you on the wrong end of some serious arms control or flight regulations.

Arduino is a pretty good choice. It was designed for people who want to do things with electronics, but aren't necessarily wanting to become software or electrical engineers, so it tries to make things as simple as possible. If you really want to use python (or any other language that isn't tied to a specific platform) you might want to look into a Rasberry Pi or a BeagleBone. They are both small boards designed to interface with hardware and run linux (which means full support for python, c, c++, or just about any other language) though they tend to lean towards one language or another. I haven't used a Rasberry PI, but I believe it has good python support. I have an old beaglebone I haven't touched in a few years. Oddly they use javascript for their preferred language, but if I recall correctly you can interface with most things via the linux filesystem, so any language should work, though performance may suffer.

Link to comment
Share on other sites

19 minutes ago, satnet said:

Actually Arduino uses C/C++, but has a pre-processor that auto-generates some of the things you normally need to do by hand (i.e. generating header files). It will gladly accept ordinary C/C++ in addition to the simplified version that only works with this pre-processor.

There are a couple of projects that use Arduino's in quadcopters, which means that there is code out there for interfacing with sensors and making real-time adjustments to maintain stable flight. Unfortunately I checked my old links and they seem to be defunct. Having said that that I'm sure there are a few projects covered on Hackaday that would help you out. 

One thing I'll point out is before you do this is check all relevant laws (nation, state/province, city). Depending on where you live, building a fully guided rocket may put you on the wrong end of some serious arms control or flight regulations.

Arduino is a pretty good choice. It was designed for people who want to do things with electronics, but aren't necessarily wanting to become software or electrical engineers, so it tries to make things as simple as possible. If you really want to use python (or any other language that isn't tied to a specific platform) you might want to look into a Rasberry Pi or a BeagleBone. They are both small boards designed to interface with hardware and run linux (which means full support for python, c, c++, or just about any other language) though they tend to lean towards one language or another. I haven't used a Rasberry PI, but I believe it has good python support. I have an old beaglebone I haven't touched in a few years. Oddly they use javascript for their preferred language, but if I recall correctly you can interface with most things via the linux filesystem, so any language should work, though performance may suffer.

A good point about arms controls I will look into that. I think I will be fine but Ill double check. I will look into Rasberry PIs aswell. 

Link to comment
Share on other sites

Thank for answering those questions: I was wondering what was wrong with this code

The idea is that I have 0 (a) and 1 (b) and if a sensor calls the function titled voltage it inserts 1 into the b spot o the function. Then if this happens it names this number c

( I know its redundant ) Then if c>0 it will do something. Without using the actual hardware when I type voltage(1) into the console I should get the "Activate Response" printed. The idea of this is that if a sensor is activated it outputs 1 which causes the rest of the code to function.

a = 0
def voltage(b):
    b+0 = c
    
if c>0:
    print("Activate Response")
        

The console is telling me that

File "C:/Users/CENSOREDNAME/Desktop/CENSOREDFILE/CENSOREDFILE/CENSOREDFILE/Sensor Code B.py", line 9

0+b = c

^

^

SyntaxError: can't assign to operator

 

Ive tried flipping the b with the 0 and replacing the 0 with an "a" either way the very first part of the function is the problem.

 

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

16 minutes ago, Cheif Operations Director said:

Thank for answering those questions: I was wondering what was wrong with this code

The idea is that I have 0 (a) and 1 (b) and if a sensor calls the function titled voltage it inserts 1 into the b spot o the function. Then if this happens it names this number c

( I know its redundant ) Then if c>0 it will do something. Without using the actual hardware when I type voltage(1) into the console I should get the "Activate Response" printed. The idea of this is that if a sensor is activated it outputs 1 which causes the rest of the code to function.

a = 0
def voltage(b):
    b+0 = c
    
if c>0:
    print("Activate Response")
        

The console is telling me that

File "C:/Users/CENSOREDNAME/Desktop/CENSOREDFILE/CENSOREDFILE/CENSOREDFILE/Sensor Code B.py", line 9

0+b = c

^

^

SyntaxError: can't assign to operator

 

Ive tried flipping the b with the 0 and replacing the 0 with an "a" either way the very first part of the function is the problem.

 

Try c = b+0

Link to comment
Share on other sites

Just now, Bill Phil said:

Try c = b+0

One second

File "C:/Users/CENSOREDNAME/CENSOREDFILE/CENSOREDFILE/CENSOREDFILE/CENSOREFILE/Sensor Code B.py", line 11, in <module>

if c>a:

 

NameError: name 'c' is not defined

 

 

In [36]:

 

In [36]: voltage(1)

a = 0
def voltage(b):
    c = b+0
   
if c>a:
    print("Activate Response")
    


       

Link to comment
Share on other sites

I got it to SORT of work

Here was the code:

a = 0
def voltage(b):
    global c
    c = b+0
    
voltage(1)

if c>a:
    print("Activate Response")

 

I still can not get it work in the console

2 minutes ago, HebaruSan said:

Add "global c" to the function before 'c' is used.

lol

You read my mind

Or I guess I read yours

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

21 minutes ago, Cheif Operations Director said:

One second

File "C:/Users/CENSOREDNAME/CENSOREDFILE/CENSOREDFILE/CENSOREDFILE/CENSOREFILE/Sensor Code B.py", line 11, in <module>

if c>a:

 

NameError: name 'c' is not defined

 

 

In [36]:

 

In [36]: voltage(1)

a = 0
def voltage(b):
    c = b+0
   
if c>a:
    print("Activate Response")
    


       

Put 

return c

in the voltage function, then instead of using c in the if statement use the function with an input.

a = 0
def voltage(b):
    c = b+0
   return c


if voltage(1)>a:
    print("Activate Response")
    

Edited by Bill Phil
Link to comment
Share on other sites

4 minutes ago, Bill Phil said:

Put 

return c

in the voltage function, then instead of using c in the if statement use the function with an input. 

That version seems to work best however it still works without be putting function(1) into the console. When I run it I get the message I only want to see when I call the function

Link to comment
Share on other sites

1 minute ago, Cheif Operations Director said:

That version seems to work best however it still works without be putting function(1) into the console. When I run it I get the message I only want to see when I call the function

Well a is zero, so anything greater than zero will satisfy the if statement.

In fact all your function does is set one variable equal to another. 

Edited by Bill Phil
Link to comment
Share on other sites

23 minutes ago, Cheif Operations Director said:

a = 0
def voltage(b):
    c = b+0
    return c  
voltage(-100)
if c>a:
    print("Activate Response")

 

still runs

Also when typing into a console it still will not print activate response

Yeah it should run. 

Again it should print “activate response” only if the if statement is satisfied. 

Are you saying that it doesn’t even when c is greater than a?

What are you using to run it? IDLE?

Link to comment
Share on other sites

Just now, Bill Phil said:

Yeah it should run. 

Again it should print “activate response” only if the if statement is satisfied.  

Are you saying that it doesn’t even when c is greater than a? 

What are you using to run it? IDLE?

It prints when c (-100) is > 0 which is obviously false. Im using SPYDER to run the code.

This was the response from the console when I typed this into it

 

In [54]: voltage(-100)

Out[54]: -100

It always prints the response

when I run the code. When I type it into the console I just get the number I typed into it back

Link to comment
Share on other sites

38 minutes ago, Cheif Operations Director said:

a = 0
def voltage(b):
    c = b+0
    return c  
voltage(-100)
if c>a:
    print("Activate Response")

 

That's... odd. I just ran that script verbatim through both the Python2 and Python3 interpreters, and both came up with (as I expected) a "name 'c' is not defined" error on line 6.

Right, so let's run through what this does line by line and see if that helps you understand what's going wrong.

Line 1: Create a new variable 'a' and assign its value to the integer 0

Line 2: define a new function 'voltage' with a single input 'b'

Line 3 (in voltage): Create a new variable 'c' in the scope of the function and assign 'b+0' to it. What this means is that the 'c' you've created will only be accessible by other stuff in the body of voltage, and not by anything outside of it.

Line 4 (in voltage): Return 'c' from the call to 'voltage'. This doesn't give stuff outside 'voltage' access to 'c'. What it does is make calls to voltage evaluate to whatever the value of 'c' was within 'voltage'.

Line 5: Call voltage with b=-100, and then do nothing with the result. What happens here is that voltage is called, it returns -100, and then that -100 is thrown away because you didn't tell Python to do anything with it.

Line 6: Compare the 'a' you declared earlier to some variable 'c' (different from the one in 'voltage'), and if so execute the condition body. Now, my Python interpreter throws an error here, since as far as it's concerned 'c' doesn't exist. Which it shouldn't, since you never defined it. It's weird that yours isn't throwing an error here.

Line 7 (in the condition started at line 6): Print "Activate Response".

Edited by IncongruousGoat
Link to comment
Share on other sites

3 minutes ago, IncongruousGoat said:

 

That's... odd. I just ran that script verbatim through both the Python2 and Python3 interpreters, and both came up with (as I expected) a "name 'c' is not defined" error on line 6.

Right, so let's run through what this does line by line and see if that helps you understand what's going wrong.

Line 1: Create a new variable 'a' and assign its value to the integer 0

Line 2: define a new function 'voltage' with a single input 'b'

Line 3 (in voltage): Create a new variable 'c' in the scope of the function and assign 'b+0' to it. What this means is that the 'c' you've created will only be accessible by other stuff in the body of voltage, and not by anything outside of it.

Line 4 (in voltage): Return 'c' from the call to 'voltage'. This doesn't give stuff outside 'voltage' access to 'c'. What it does is make calls to voltage evaluate to whatever the value of 'c' was within 'voltage'.

Line 5: Call voltage with b=-100, and then do nothing with the result. What happens here is that voltage is called, it returns -100, and then that -100 is thrown away because you didn't tell Python to do anything with it.

Line 6: Compare the 'a' you declared earlier to some variable 'c' (different from the one in 'voltage'), and if so execute the condition body. Now, my Python interpreter throws an error here, since as far as it's concerned 'c' doesn't exist. Which it shouldn't, since you never defined it. It's weird that yours isn't throwing an error here.

Line 7 (in the condition started at line 6): Print "Activate Response".

I got this to work as well

a = 0
def voltage(b):
    global c  
    c = b+0

if c>a:
    print("Activate Response")
    

Im reading your response

Link to comment
Share on other sites

12 minutes ago, Cheif Operations Director said:

It prints when c (-100) is > 0 which is obviously false. Im using SPYDER to run the code.

This was the response from the console when I typed this into it

 

In [54]: voltage(-100)

Out[54]: -100

It always prints the response

when I run the code. When I type it into the console I just get the number I typed into it back

It might be printing it because of the return statement and not the if statement. Comment out the if statement and run it again.

Link to comment
Share on other sites

a = 0
def voltage(b):
    global c
    c= b+0
    return c  

if c>a:
    print("Activate Response") 

In terms of operation here is the debugged version

10 minutes ago, IncongruousGoat said:

Line 5: Call voltage with b=-100, and then do nothing with the result. What happens here is that voltage is called, it returns -100, and then that -100 is thrown away because you didn't tell Python to do anything with it.

The result is suppose to become the c value.

7 minutes ago, Bill Phil said:

It might be printing it because of the return statement and not the if statement. Comment out the if statement and run it again.

I did that and in terms of the result I got from the console it was the same thing with or without it (the if > statement)

a = 0
def voltage(b):
    global c
    c= b+0
    return c  

if c>a:
    print("Activate Response"

How do I tell the code to only do the if statement if b is greater than 0

Once this is done the problem is resolved

I think

Link to comment
Share on other sites

3 minutes ago, Cheif Operations Director said:

The result is suppose to become the c value.

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.

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