[CentralOH] Refactoring Motor Control Code?

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Sun Dec 8 20:36:08 CET 2013


On Sun, 8 Dec 2013 11:09:37 -0500, Louis Bogdan <looiebwv at gmail.com> wrote:

> As I ... am totally new to Python ...

Come to the dojos. They are good for beginners (and others also).
We can explore really bad ways and some OK ways to do things. 
Download big files before coming. 

> ... proposed “subroutine” ...

What you have is not a subroutine (which would be called a 
"function" in Python), but could become one. 

> num = 1.2346
> 
> a = num(-1)
> 
> b = num(-2)

I concur with Fitzgerald. 

> if a >7:
> 
>      a = 0
> 
>      b+ =1
> 
> elif a<3:
> 
>      a = 0
> 
> else:
> 
>       a = 5

goal = 5  # Unit is what?
tolerance = 2  # Unit is what?

error = a - goal
if error > +tolerance:
    a = 0
    n_too_high += 1
elif error < -tolerance:
    a = 0
else:
    a = goal

The asymmetry between a being too high or too low is curious.
Why don't you count the number of times that a is too low?

error = a - goal
if abs(error) <= tolerance:
    a = goal
else:
    a = 0
    if error > 0:
        n_too_high += 1
    else:
        n_too_low += 1

Magic numbers are evil[2]. Give them meaningful names. 

Read and follow PEP 8.[1]

Names are important. Rename b to something meaningful like 
number_of_times_too_high or n_too_high. 
Choose a better name for goal.
Document the units for each name. 

> This routine also has application in several other projects I 
> have under consideration. Would something like this, if it 
> works, be a Python library item?

What you showed is not a routine (i.e. function), but could 
become one. If you use it from many modules, then it probably 
should go into a separate module to be imported. 

> As each stepper motor step equals .0005” of movement, 
> with the above I can get .0002” absolute positional accuracy 
> with a .0005” control increment. 

It is curius that .0005" is not an even multiple 
of .0002". 
Are you microstepping the stepper motors? 
Do you have positioning feedback? 
Thermal expansion is fun.
Are you confusing accuracy with resolution? 

At least one folk who attends the dojos has experience with 
embedded programming and moving stuff. Bresenham's 
algorithm[4] is fun. 

Again, document the units for each name in the code.

> I have looked at some of the Python “round” statements and 
> examples and don’t fully understand them ...

It helps to know what you are trying to accomplish 
before figuring out _how_ to accomplish it.

What are you trying to accomplish? 
What are the inputs?
What are the outputs?

In answering the above, try to avoid speaking in terms 
that suggest a how to accomplish whatever your goal is.

You might recognize similar statements from your career. 

Let's have some fun with bad tricky ways of doing stuff.

too_much = [0] *2
...
error = a - goal
if abs(error) <= tolerance:
    a = goal
else:
    a = 0
    #  The following is too tricky, so do not do it. 
    too_much[error < 0] += 1

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

error = a - goal

#  The following is also too tricky.
a = goal if abs(error) <= tolerance else 0

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

> What do I need to buy to get started with RPi? 

enthusiasm (Urban Meyer sells this)
Raspberry Pi B (avoid the A model)
power supply
SDHC card (get a fast one!)
A display that handles HDMI input, and HDMI cable could be helpful.
USB keyboard and USB mouse could be helpful.
Network connection could be helpful.
Go to Microcenter on Bethel.

Also read "The Zen of Python"[3].

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

n = {-1: 0, 0: 0, 1: 0}
n = dict((i, 0) for i in (-1, 0, 1))
n = dict((i-1, 0) for i in range(3))
...
#  And finally, monsieur, a wafer-thin mint.
#  It's just two wafer thin lines.
#  Just the one, sir... voila... bon appétit...
n[min(1, max(-1, ((a - (goal - tolerance)) / (2 * tolerance + 1))))] += 1
a = goal * (not ((a - (goal - tolerance)) / (2 * tolerance + 1)))

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

[1] http://www.python.org/dev/peps/pep-0008/
[2] https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants
[3] http://www.python.org/dev/peps/pep-0020/
    Extra bonus points for reading ZMM
    https://en.wikipedia.org/wiki/Zen_and_the_Art_of_Motorcycle_Maintenance
[4] https://en.wikipedia.org/wiki/Bresenham's_line_algorithm


More information about the CentralOH mailing list