Help with motor control - Sorry I am just learning

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
sclark970
Posts: 3
Joined: Wed Jan 29, 2020 8:18 pm

Help with motor control - Sorry I am just learning

Post by sclark970 » 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
:D

chrismas9
Posts: 152
Joined: Wed Jun 25, 2014 10:07 am

Re: Help with motor control - Sorry I am just learning

Post by chrismas9 » Wed Jan 29, 2020 11:43 pm

I like to learn by example. There are plenty of robot kits with sample code. Just search "micro:bit robot MicroPython". Here is one example:

https://github.com/KitronikLtd/micropyt ... cs?files=1

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Help with motor control - Sorry I am just learning

Post by pythoncoder » Thu Jan 30, 2020 7:59 am

@sclark970 If you are new to the Python language I would recommend following a course, either online or from a textbook. Work through the examples on a PC.
Peter Hinch
Index to my micropython libraries.

sclark970
Posts: 3
Joined: Wed Jan 29, 2020 8:18 pm

Re: Help with motor control - Sorry I am just learning

Post by sclark970 » Sun Feb 02, 2020 8:26 pm

Yes thanks for that.
I have all ready written code to control the robot buggy using two micro:bits and Micro Python. I was sort of looking to see if there was anyone out there with some tips and tricks to make the code shorter and improve response time. I do not want to use a bought in motor controller and pre set up functions as we build the motor controller from the ground up with transistors before moving onto an H bridge chip.
Perhaps I should try a different forum linked to robotics.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Help with motor control - Sorry I am just learning

Post by jimmo » Sun Feb 02, 2020 11:03 pm

sclark970 wrote:
Wed Jan 29, 2020 8:51 pm
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?
Yes, I think so. Pretty much exactly what you wrote looks like the right idea. I have written this exact code many times with students.

There's a few ways to write the "motor_control" function. Maybe the simplest starting point is to make it take the dual H-bridge inputs (A1 B1 A2 B2) such that you can write:

Code: Select all

motor_control(1, 0, 1, 0)  # forward
motor_control(0, 1, 0, 1)  # reverse
motor_control(1, 0, 0, 1)  # turn right
motor_control(0, 1, 1, 0)  # turn left
If you do it the way you suggest then you'll have to write some if statements to figure out forward/left/right.
sclark970 wrote:
Wed Jan 29, 2020 8:51 pm
So is this even possible?
Would it be more efficient?
Would it produce faster running code?
Am I barking up the wrong tree?
Possible, yes.

Efficient... maybe. Shorter code generally means it'll take up less RAM (which is very important on the micro:bit).

Faster, possibly. The expensive part is calling the function. Adding arguments isn't too bad.

sclark970
Posts: 3
Joined: Wed Jan 29, 2020 8:18 pm

Re: Help with motor control - Sorry I am just learning

Post by sclark970 » Sun Feb 16, 2020 8:11 am

Thanks Jimmo. I have been working on things a bit when I have had the chance. I think I have now got enough knowledge of functions to get down to some practical fun and games. Yes I now think I can make the method you explained work. Also found some cool ways to shorten down my code. Thanks for your support. Much of the stuff I have done in the past in relation to structuring code seems to have transferred over quite well. However, the setting up of functions in Python is new / different. I am getting a glimpse of what it can do. I am very excited about this.
Thanks mate.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Help with motor control - Sorry I am just learning

Post by jimmo » Sun Feb 16, 2020 11:41 pm

sclark970 wrote:
Sun Feb 16, 2020 8:11 am
Thanks Jimmo. I have been working on things a bit when I have had the chance. I think I have now got enough knowledge of functions to get down to some practical fun and games. Yes I now think I can make the method you explained work. Also found some cool ways to shorten down my code. Thanks for your support. Much of the stuff I have done in the past in relation to structuring code seems to have transferred over quite well. However, the setting up of functions in Python is new / different. I am getting a glimpse of what it can do. I am very excited about this.
Thanks mate.
Thanks! Great to hear. Let us know how you go and feel free to post any follow up questions.

Post Reply