from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
class SimpleRobotArm:
def __init__(self):
self.name= "Simple Robot Arm"
self.shoulder = 90.0
self.elbow = 0.0
self.arm = 0.0
self.wrist = 0.0
self.finger1= 0.0
self.finger2= 0.0
self.finger3= 0.0
def run(self):
glutInit(sys.argv) # initialise the system
# Configure inital display mode
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
# Set up and display initial window
glutInitWindowSize(1000,1000)
glutInitWindowPosition(100,100)
glutCreateWindow(self.name) # See the __init__ method for self.name
# Initial colour and shading model
glClearColor(0.0,0.0,0.0,0.0)
glShadeModel(GL_FLAT)
# Register callback methods. The arguments are the names of
# methods defined below.
glutDisplayFunc(self.display)
glutReshapeFunc(self.reshape)
# glutMouseFunc(self.mouse) # not needed here
glutKeyboardFunc(self.keys)
# Launch the OGL event processing loop
glutMainLoop()
def display(self):
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef (-4.0, -3.0, -3.0);
glRotatef (self.shoulder, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.2, 0.5);
glutWireCube (1.0);
glPopMatrix();
glTranslatef (1.0, 0.0, 0.0);
glRotatef (self.elbow, 0.0, 0.0, 0.8);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.2, 0.5);
glutWireCube (1.0);
glPopMatrix();
glTranslatef (1.0, 0.0, 0.0);
glRotatef (self.arm, 0.0, 1.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.2, 0.5);
glutWireCube (1.0);
glPopMatrix();
glTranslatef (1.0, 0.0, 0.0);
glRotatef (self.wrist, 0.0, 0.0, 0.0);
glTranslatef (0.3, 0.0, 0.0);
glPushMatrix();
glScalef (0.6, 0.7, 0.8);
glutWireCube (1.0);
glPopMatrix();
glTranslatef (0.0, 0.3, 0.2);
glRotatef (self.finger1, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (1.5, 0.1, 0.1);
glutWireCube (1.0);
glPopMatrix();
glTranslatef (0.0, 0.0, -0.5);
glRotatef (self.finger2, 0.0, 0.0, 1.0);
glTranslatef (0.0,0.0,0.0);
glPushMatrix();
glScalef (1.5, 0.1, 0.1);
glutWireCube (1.0);
glPopMatrix();
glTranslatef(0.0,-0.6,0.3)
glRotatef (self.finger3, 0.0, 0.0, 1.0);
glTranslatef (0.0,0.0,0.0);
glPushMatrix();
glScalef (1.5, 0.1, 0.1);
glutWireCube (1.0);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
def reshape(self,w,h):
glViewport (0, 0, w, h)
glMatrixMode (GL_PROJECTION)
glLoadIdentity()
gluPerspective(65.0, w/h, 1.0, 20.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef (0.0, 0.0, -6.0)
def keys(self,*args):
key = args[0]
if (key == 's'):
self.shoulder = (self.shoulder+5) % 360
elif (key=='S'):
self.shoulder = (self.shoulder-5) % 360
elif (key=='e'):
self.elbow = (self.elbow+5) % 360
elif (key=='E'):
self.elbow = (self.elbow-5) % 360
elif (key=='w'):
self.arm = (self.arm+5) % 360
elif (key=='W'):
self.arm = (self.arm-5) % 360
elif (key=='q'):
self.finger1 = (self.finger1-5) % 360
elif (key=='Q'):
self.finger1 = (self.finger1+5) % 360
elif (key=='a'):
self.finger2 = (self.finger2-5) % 360
elif (key=='A'):
self.finger2 = (self.finger2+5) % 360
elif (key=='x'):
self.finger3 = (self.finger3-5) % 360
elif (key=='X'):
self.finger3 = (self.finger3+5) % 360
glutPostRedisplay()
if __name__ == '__main__':
app = SimpleRobotArm()
app.run()
Robot Arm Opengl-Python
Page 1 of 14 Replies - 2609 Views - Last Post: 23 May 2012 - 06:47 AM
#1
Robot Arm Opengl-Python
Posted 21 March 2011 - 08:03 AM
hello people. I am a beginner with opengl. I am writing this code in pyscripter. I am trying to get the fingers to move indepentantly or at least finger1 to move with finger2 to close and catch things but I cant figure out how to do that. Basically I am trying to move fingers 1 and 2 but finger 3 to stay at its position or close towards the other 2 fingers. this is the code I have until now.
Replies To: Robot Arm Opengl-Python
#2
Re: Robot Arm Opengl-Python
Posted 21 March 2011 - 09:42 AM
Moving the fingers of what exactly? Not enough info to go on here.
#3
Re: Robot Arm Opengl-Python
Posted 21 March 2011 - 09:46 AM
ButchDean, on 21 March 2011 - 09:42 AM, said:
Moving the fingers of what exactly? Not enough info to go on here. 
well i am trying to rotate the fingers of the robot arm so it will be able to catch things. There are 3 fingers at the top of the arm after the "wrist" which is not a moving part. when I rotate finger1 finger2 and finger3 rotates also. when i rotate finger2 finger3 rotates with it.is there a way to rotate finger1 and 2 and finger 3 stay at its place?
#4
Re: Robot Arm Opengl-Python
Posted 21 March 2011 - 09:59 AM
nevermind i ve figured it out
create finger3 first
and then the other 2.
#5
Re: Robot Arm Opengl-Python
Posted 23 May 2012 - 06:47 AM
#!/usr/bin/env python
#
# This code was created by Richard Campbell '99 (ported to Python/PyOpenGL by John Ferguson 2000)
#
# The port was based on the PyOpenGL tutorial module: dots.py
#
# If you've found this code useful, please let me know (email John Ferguson at hakuin@voicenet.com).
#
# See original source and C based tutorial at http://nehe.gamedev.net
#
# Note:
# -----
# This code is not a good example of Python and using OO techniques. It is a simple and direct
# exposition of how to use the Open GL API in Python via the PyOpenGL package. It also uses GLUT,
# which in my opinion is a high quality library in that it makes my work simpler. Due to using
# these APIs, this code is more like a C program using function based programming (which Python
# is in fact based upon, note the use of closures and lambda) than a "good" OO program.
#
# To run this code get and install OpenGL, GLUT, PyOpenGL (see http://www.python.org), and PyNumeric.
# Installing PyNumeric means having a C compiler that is configured properly, or so I found. For
# Win32 this assumes VC++, I poked through the setup.py for Numeric, and chased through disutils code
# and noticed what seemed to be hard coded preferences for VC++ in the case of a Win32 OS. However,
# I am new to Python and know little about disutils, so I may just be not using it right.
#
# BTW, since this is Python make sure you use tabs or spaces to indent, I had numerous problems since I
# was using editors that were not sensitive to Python.
#
# Modified on May 2nd,2004 by Travis Wells to fix some GLUT issues and missing import
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
# Some api in the chain is translating the keystrokes to this octal string
# so instead of saying: ESCAPE = 27, we use the following.
ESCAPE = '\033'
# Number of the glut window.
window = 0
# A general OpenGL initialization function. Sets all of the initial parameters.
def InitGL(Width, Height): # We call this right after our OpenGL window is created.
glClearColor(0.0, 0.0, 0.0, 0.0) # This Will Clear The Background Color To Black
glClearDepth(1.0) # Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS) # The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST) # Enables Depth Testing
glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION)
glLoadIdentity() # Reset The Projection Matrix
# Calculate The Aspect Ratio Of The Window
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
def ReSizeGLScene(Width, Height):
if Height == 0: # Prevent A Divide By Zero If The Window Is Too Small
Height = 1
glViewport(0, 0, Width, Height) # Reset The Current Viewport And Perspective Transformation
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
# The main drawing function.
def DrawGLScene():
# Clear The Screen And The Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity() # Reset The View
# since this is double buffered, swap the buffers to display what just got drawn.
glutSwapBuffers()
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
def keyPressed(*args):
# If escape is pressed, kill everything.
if args[0] == ESCAPE:
glutDestroyWindow(window)
sys.exit()
def main():
global window
# For now we just pass glutInit one empty argument. I wasn't sure what should or could be passed in (tuple, list, ...)
# Once I find out the right stuff based on reading the PyOpenGL source, I'll address this.
glutInit("")
# Select type of Display mode:
# Double buffer
# RGBA color
# Alpha components supported
# Depth buffer
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
# get a 640 x 480 window
glutInitWindowSize(640, 480)
# the window starts at the upper left corner of the screen
glutInitWindowPosition(0, 0)
# Okay, like the C version we retain the window id to use when closing, but for those of you new
# to Python (like myself), remember this assignment would make the variable local and not global
# if it weren't for the global declaration at the start of main.
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
# Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
# set the function pointer and invoke a function to actually register the callback, otherwise it
# would be very much like the C version of the code.
glutSetDisplayFuncCallback(DrawGLScene)
glutDisplayFunc()
# Uncomment this line to get full screen.
#glutFullScreen()
# When we are doing nothing, redraw the scene.
glutSetIdleFuncCallback(DrawGLScene)
glutIdleFunc()
# Register the function called when our window is resized.
glutSetReshapeFuncCallback(ReSizeGLScene)
glutReshapeFunc()
# Register the function called when the keyboard is pressed.
glutSetKeyboardFuncCallback(keyPressed)
glutKeyboardFunc()
# Initialize our window.
InitGL(640, 480)
# Start Event Processing Engine
glutMainLoop()
# Print message to console, and kick off the main to get it rolling.
print "Hit ESC key to quit."
main()
I am a newbie programmer. The error I am getting after running this program in pyscripter is :ImportError: No module named OpenGL.GL. Can someone please tell me how do install stuff to make this program work. I would really appreciate your help thanks
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|