Tag Archives: pyglet

Robot Arms, Chaotic Python, and London

So I’ve moved down to London, which is kind of like Manchester except the buses smell slightly better. In a hostel at the moment with a 1Mb connection and a single router for the whole place so internet is somewhat intermittent.

While i’m without tools, building materials, and internetty distractions I’ve finally decided to learn some python, which, i have to say, is starting to grow on me quite a lot. It’s like programming is fun again rather than just a tool to get stuff done with. So without further ado here is my first python program. It uses pyglet (which is awesome) to plot the points of this equation: x_next = x*r*(1-x)

#! /usr/bin/env python

from pyglet.gl import *
from pyglet.graphics import *
from pyglet.window import key

class HelloWorldWindow(pyglet.window.Window):

    def __init__(self):
         super(HelloWorldWindow, self).__init__()
         self.param = 2.8
         self.start_val = .34
         self.y_vals =[0]
         self.create_chaos()
         self.label = pyglet.text.Label('PgUp/PgDown-Starting Value Up/Down-Lambda Value')

     def on_draw(self):
         glClear(GL_COLOR_BUFFER_BIT)
         glLoadIdentity()
         for count in range(0,800):
             pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,('v2i', (count, int(self.y_vals[count]*200))))
         self.label.draw()

     def create_chaos(self):
         self.y_vals =[self.start_val]
         r = self.param
         for x in range(0,800):
             self.y_vals.append(self.y_vals[x]*r*(1-self.y_vals[x]))

     def on_key_press(self, symbol, modifiers):
         if symbol == key.UP:
             print ['Lambda: ', self.param]
             self.param += 0.05
             if self.param >= 4:
                 self.param = 4
         elif symbol == key.DOWN:
             self.param -= 0.05
             print ['Lambda: ', self.param]
         elif symbol == key.PAGEUP:
             self.start_val += 0.05
             if self.start_val > 1:
                 self.start_val =1
             print ['Lambda: ', self.start_val]
         elif symbol== key.PAGEDOWN:
             self.start_val -= 0.05
             if self.start_val < 0:
                 self.start_val =0
             print ['Lambda: ', self.start_val]
         self.create_chaos()

if __name__ == '__main__':
     window = HelloWorldWindow()
     pyglet.app.run()

PgUp/PgDown adjust the starting value of x while the up/down keys adjust the value of r.

The equation is from the book Chaos by James Gleick and demonstrates a chaotic system (keep pressing up and see how the system becomes stable in multiple states before turning chaotic).

The system stable in one state


The system stable in multiple states


Things getting chaotic

I really can’t describe it as eloquently and simply as the book, which i highly recommend reading.

In other exciting news i bought a hydraulic robot arm from a car boot sale for a mere 40 pounds!

My good friend lon who helped me transport the arm to my parents (hello lon! Your probably the only one whos gonna read this 😉 )


Me attending to the arm

Kind of frustrating not being able to play with it being in London an all, but with my new found python&pyglet skillz i’m gonna try and write some inverse kinematics code to control it when i finally do get my hands on it. If i get some free time/ internet i’ll post some details about it.

Advertisements