Jump to content

Recommended Posts

Posted

I'm back with another (probably easy) python problem.

 

I've dusted off the robot arm and plugged it into the 'pi.

 

I've got the following code and it works fine...

 

# ROBOT ARM CONTROL PROGRAM
# import the USB and Time libraries into Python
import usb.core, usb.util, time
# Allocate the name 'RoboArm' to the USB device
RoboArm = usb.core.find(idVendor=0x1267,idProduct
=0x0000)
# Check if the arm is detected and warn if not
if RoboArm is None:
raise ValueError("Arm not found")
# Create a variable for duration
Duration=1
# Define a procedure to execute each movement
def MoveArm(Duration, ArmCmd):
# Start the movement
RoboArm.ctrl_transfer(0x40,6,0x100,0,ArmCmd,
1000)
# Stop movement after waiting specified time
time.sleep(Duration)
ArmCmd=[0,0,0]
RoboArm.ctrl_transfer(0x40,6,0x100,0,ArmCmd,
1000)
# Give the arm some commands
MoveArm(1,[0,1,0]) # Rotate Base Anti-clockwise
MoveArm(1,[0,2,0]) # Rotate Base Clockwise
MoveArm(1,[64,0,0]) # Shoulder Up
MoveArm(1,[128,0,0]) # Shoulder Down
MoveArm(1,[16,0,0]) # Elbow Up
MoveArm(1,[32,0,0]) # Elbow Down
MoveArm(1,[4,0,0]) # Wrist Up
MoveArm(1,[8,0,0]) # Wrist Down
MoveArm(1,[2,0,0]) # Grip Open
MoveArm(1,[1,0,0]) # Grip Close
MoveArm(1,[0,0,1]) # Light On
MoveArm(1,[0,0,0]) # Light Off

 

I want to develop this code so that the arm responds to key presses....

 

So I imagine it will be:

 

when keyboardinput(a)=true MoveArm(1, [0,1,0])

 

HOW CLOSE AM I?!?!! it's the keyboard input thing that I've made up... what is the actual command?

 

Thanks in anticipation!

Posted (edited)

This is actually one of those simple things that Python really sucks at - you can't do what you're imagining in any simple way.

 

You'll need a library to do it, and threading will help. There's a code snippet on Rosetta Code you can use: Keyboard input/Keypress check - Rosetta Code

 

Don't worry too much about the implementation of getch(), just focus on the loop to see how you can adapt it to work with your own code. Essentially, the code sets a thread running that captures the character pressed continuously, allowing you to read off the current key pressed. Not sure if this will work for the arrow keys, but you could 'spoof' it by using 2, 4, 6, 8 on the numpad.

 

EDIT: meant to add, you have the concept right, it's just the actual capture of keyboard input that's awkward in Python. But yeah, implement that getch() snippet from Rosetta Code and issue commands from your loop depending on current pressed key. No switch statements in Python either so you'll need a string of if/else statements (and I do recommend using if/else rather than just if - as getch() will run in a separate thread, if you don't use 'else' then there's potential for more than one key to be detected per loop which could mess up some logic you add later on - using else will make sure only one key's actions are performed per loop)

 

EDIT2: that was clear as mud, so to calrify in pseudo-code, you'd do something like this:

 

if char is not None:
   if char == '8':
       moveUp()
   elif char == '2':
       moveDown()
   elif char == '4':
       moveLeft()
   elif char == '6':
       moveRight()

Edited by LosOjos
  • Thanks 1
Posted (edited)

Python is fair $#!7 really isn't it?

 

Hah... I actually already had that window open... just no idea what it means.

 

I am rather short on patience. This could be my problem and a reminder of why I didn't become a programmer!

Edited by Joanne
Posted
Python is fair $#!7 really isn't it?

 

Hah... I actually already had that window open... just no idea what it means.

 

I am rather short on patience. This could be my problem and a reminder of why I didn't become a programmer!

 

Like any language, it has strong and weak points. It's a great language for learning as it's very flexible and there are tons of libraries out there plus hundreds of examples, but it's certainly not without it's shortcomings.

 

Stick with it, it'll pay off. You might not understand what that code does exactly right now, but if you can adapt it to work for you then don't worry. It's not great having a load of code you don't understand, but it's really no different to using libraries - you can always dig in to the detail when you're more confident :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...