EpixP3
Posts: 6
Joined: Sat Apr 18, 2015 7:07 pm

[NEED HELP] Python code (about Robotic arm) fix

Sat Apr 18, 2015 7:17 pm

Hi,

I have a problem with coding, i want to reverse the robotic arm to its standard (first) pozition (sorry for my english, i'm slovak btw.). Here's my code

Code: Select all

import RPi.GPIO as g
import time as t
import Tkinter as tk

m5r = 40
m5l = 38
m4l = 37
m4r = 36
m3l = 35
m3r = 33
m2l = 32
m2r = 18
m1l = 29
m1r = 26
led = 24
p5r = 0
p4r = 0
p3r = 0
p2r = 0
p1r = 0
p5l = 0
p4l = 0
p3l = 0
p2l = 0
p1l = 0


g.setmode(g.BOARD)
g.setup(m5r, g.OUT)
g.setup(m4r, g.OUT)
g.setup(m3r, g.OUT)
g.setup(m2r, g.OUT)
g.setup(m1r, g.OUT)
g.setup(m5l, g.OUT)
g.setup(m4l, g.OUT)
g.setup(m3l, g.OUT)
g.setup(m2l, g.OUT)
g.setup(m1l, g.OUT)
g.setup(led, g.OUT)


def test():
    g.output(m5r,True)
    t.sleep(1)
    g.output(m5r,False)
    g.output(m5l,True)
    t.sleep(1)
    g.output(m5l,False)
    g.output(m4r,True)
    t.sleep(1)
    g.output(m4r,False)
    g.output(m4l,True)
    t.sleep(1)
    g.output(m4l,False)
    g.output(m3r,True)
    t.sleep(1)
    g.output(m3r,False)
    g.output(m3l,True)
    t.sleep(1)
    g.output(m3l,False)
    g.output(m2r,True)
    t.sleep(1)
    g.output(m2r,False)
    g.output(m2l,True)
    t.sleep(1)
    g.output(m2l,False)
    g.output(m1r,True)
    t.sleep(1)
    g.output(m1r,False)
    g.output(m1l,True)
    t.sleep(1)
    g.output(m1l,False)
    g.output(led,True)
    t.sleep(2)
    g.output(led,False)
test()
def fixm5():
    if(p5l>p5r):
        p5l=p5l-p5r
    if(p5r>p5l):
        p5r=p5r-p5l
    while p5l != 0:
        g.output(m5l, True)
        t.sleep(1)
        g.output(m5l, False)
        p5l=p5l-1
    while p5r != 0:
        g.output(m5r, True)
        t.sleep(1)
        g.output(m5r, False)
        p5r=p5r-1
def onKeyPress(event):
    if(event.char == "l"):
        test()
    if(event.char == "c"):
        fixm5()
    if(event.char == "x"):
        g.output(m5r,True)
        t.sleep(1)
        g.output(m5r,False)
        p5r=p5r+1
    if(event.char == "z"):
        g.output(m5l,True)
        t.sleep(1)
        g.output(m5l,False)
        p5l=p5l+1
    if(event.char == "q"):
        g.output(m4r,True)
        t.sleep(1)
        g.output(m4r,False)
        p4r=p4r+1
    if(event.char == "a"):
        g.output(m4l,True)
        t.sleep(1)
        g.output(m4l,False)
        p4l=p4l+1
    if(event.char == "w"):
        g.output(m3r,True)
        t.sleep(1)
        g.output(m3r,False)
        p3r=p3r+1
    if(event.char == "s"):
        g.output(m3l,True)
        t.sleep(1)
        g.output(m3l,False)
        p3l=p3l+1
    if(event.char == "d"):
        g.output(m2r,True)
        t.sleep(1)
        g.output(m2r,False)
        p2r=p2r+1
    if(event.char == "e"):
        g.output(m2l,True)
        t.sleep(1)
        g.output(m2l,False)
        p2l=p2l+1
    if(event.char == "r"):
        g.output(m1r,True)
        t.sleep(1)
        g.output(m1r,False)
        p1r=p1r+1
    if(event.char == "f"):
        g.output(m1l,True)
        t.sleep(1)
        g.output(m1l,False)
        p1l=p1l+1
    if(event.char == "g"):
        g.output(led,True)
        t.sleep(1)
        g.output(led,False)

root = tk.Tk()
root.bind('<KeyPress>', onKeyPress)
root.mainloop()

g.cleanup()

I have the problem with:

Code: Select all

def fixm5():
    if(p5l>p5r):
        p5l=p5l-p5r
    if(p5r>p5l):
        p5r=p5r-p5l
    while p5l != 0:
        g.output(m5l, True)
        t.sleep(1)
        g.output(m5l, False)
        p5l=p5l-1
    while p5r != 0:
        g.output(m5r, True)
        t.sleep(1)
        g.output(m5r, False)
        p5r=p5r-1
If you need additional info, you can allways ask.

Thanks!

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: [NEED HELP] Python code (about Robotic arm) fix

Sat Apr 18, 2015 8:56 pm

hello, I notice that you have not taken the time to comment your code. Sometimes when I am having trouble with my own code I will go back and comment it... leave myself notes about how its organized, what variables are for, what specific routines do, and how they do it... often (and I do mean often) I will find my own coding error by slowing down and documenting what my code is supposed to do I will find the place where it is not doing what I first thought!

Typically when you move a robotic arm you need to have a way to find rest position. Then, you have several options for moving the arm: 1) keep track of the 'steps' you moved in any axis, 2) move until you have a sensor interrupt, or 3) move in time. If you know how to move the arm at all you should without much trouble be able to move it to rest position, or return it to a previous position. The best way to learn is to do. Be patient, and try for a while. You'll get it.

But, seriously, before you get much further in your project, document your code. Use liberal comments; comment the code as though you need to come back to this in six months and you don't want to have to start over.... or, you want someone else (on the net or otherwise) to be able to maintain your code, or help you with it, and so you want them to be able to read and understand what your project is doing. Commenting your code is the best way to accomplish those goals.
marcus
:ugeek:

Return to “General discussion”