Help with motor control - Sorry I am just learning
Posted: Wed Jan 29, 2020 8:51 pm
Hi.
I am new to micro python.
I have just had my head blown away by the use of arguments in functions. I have done some control programming in the past linked to electronics. This has mainly consisted of checking inputs, digital or analog, and then switching on and off outputs as a result. Many years ago I did a bit of machine code stuff but I have forgotten all of that. My limited brain and experience is sort of scratching at this question and you may be able to tell me if I am barking up the wrong tree and save me a bit of time.
If I am building say a remote control robot buggy for example using the micro:bit. My code would have consisted of a layout such as-
define functions / sub routines
define variables
MAIN CODE
If condition one is true then call function X
Else if condition two is true call function Y
Else call function Z
Even using this approach with functions for forwards, turn right, turn left, reverse, turn weapon on, turn weapon off, read input from micro switch the code still ends up quite long. After reading about the use of functions and arguments would it be possible to shorten down the code so that rather than having the above approach you simply call a function with arguments added to it in some way?
So something like-
Variable for received signal (stores instruction to turn)
def motor_control(forward, right, left):
output(forward, right, left)
MAIN CODE
if variable = 1
motor_control(1,0,0)
else if variable = 2
motor_control(0,1,0)
else if variable=3
motor_control(0,0,1)
else
motor_control(0,0,0)
I am very sorry if my question is stupid. I am a teacher who is trying to learn how to teach this content because very few other people seem to want to. I feel it is vital that kids get the chance to learn this type of technology. I just need to get to the point where I can help them enough so that they no longer need me. Any advice would be welcome.
So is this even possible?
Would it be more efficient?
Would it produce faster running code?
Am I barking up the wrong tree?
Thanks
I am new to micro python.
I have just had my head blown away by the use of arguments in functions. I have done some control programming in the past linked to electronics. This has mainly consisted of checking inputs, digital or analog, and then switching on and off outputs as a result. Many years ago I did a bit of machine code stuff but I have forgotten all of that. My limited brain and experience is sort of scratching at this question and you may be able to tell me if I am barking up the wrong tree and save me a bit of time.
If I am building say a remote control robot buggy for example using the micro:bit. My code would have consisted of a layout such as-
define functions / sub routines
define variables
MAIN CODE
If condition one is true then call function X
Else if condition two is true call function Y
Else call function Z
Even using this approach with functions for forwards, turn right, turn left, reverse, turn weapon on, turn weapon off, read input from micro switch the code still ends up quite long. After reading about the use of functions and arguments would it be possible to shorten down the code so that rather than having the above approach you simply call a function with arguments added to it in some way?
So something like-
Variable for received signal (stores instruction to turn)
def motor_control(forward, right, left):
output(forward, right, left)
MAIN CODE
if variable = 1
motor_control(1,0,0)
else if variable = 2
motor_control(0,1,0)
else if variable=3
motor_control(0,0,1)
else
motor_control(0,0,0)
I am very sorry if my question is stupid. I am a teacher who is trying to learn how to teach this content because very few other people seem to want to. I feel it is vital that kids get the chance to learn this type of technology. I just need to get to the point where I can help them enough so that they no longer need me. Any advice would be welcome.
So is this even possible?
Would it be more efficient?
Would it produce faster running code?
Am I barking up the wrong tree?
Thanks