import httplib
import base64
import StringIO
import threading
import time
import wx
class Trendnet():
def __init__(self, ip='1.1.1.1', username='admin', password='admin'):
self.IP = ip
self.Username = username
self.Password = password
self.Connected = False
def Connect(self):
if self.Connected == False:
try:
print 'Atempting to connect to camera', self.IP, self.Username, self.Password
h = httplib.HTTP(self.IP)
h.putrequest('GET','/cgi/mjpg/mjpeg.cgi')
h.putheader('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (self.Username, self.Password))[:-1])
h.endheaders()
errcode, errmsg, headers = h.getreply()
self.File = h.getfile()
print 'Connected!'
self.Connected = True
except:
print 'Unable to connect!'
self.Connected = False
def Disconnect(self):
self.Connected = False
print 'Camera Disconnected!'
def Update(self):
if self.Connected:
s = self.File.readline() # '--myboundry'
s = self.File.readline() # 'Content-Length: #####'
framesize = int(s[16:])
s = self.File.read(framesize) # jpeg data
while s[0] != chr(0xff):
s = s[1:]
return StringIO.StringIO(s)
class CameraPanel(wx.Panel):
def __init__(self, parent, camera):
wx.Panel.__init__(self, parent, id=wx.ID_ANY, style=wx.SIMPLE_BORDER)
self.Camera = camera
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def OnEraseBackground(self, event):
pass
def OnPaint(self, event):
dc = wx.BufferedPaintDC(self)
if self.Camera.Connected == True:
try:
stream = self.Camera.Update()
if stream != None:
img = wx.ImageFromStream(stream)
bmp = wx.BitmapFromImage(img)
dc.DrawBitmap(bmp, 0, 0, True)
except:
pass
else:
dc.SetBrush(wx.WHITE_BRUSH)
dc.DrawRectangle(-1, -1, 620, 480)
dc.DrawCircle(340, 270, 5)
dc.DrawCircle(280, 270, 5)
if __name__ == '__main__':
def CamThread():
while True:
campanel.Refresh()
time.sleep(.01)
app = wx.App(0)
wx.Log_SetActiveTarget(wx.LogStderr())
frame = wx.Frame(parent=None, id=wx.ID_ANY, title='UpCam.py', style=wx.DEFAULT_FRAME_STYLE & ~wx.RESIZE_BORDER)
camera = Trendnet('192.168.1.102')
camera.Connect()
campanel = CameraPanel(frame, camera)
campanel.SetSize((620,480))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(campanel, 1, wx.EXPAND|wx.ALL, 5)
frame.SetSizer(sizer)
frame.Fit()
frame.Show(True)
thread = threading.Thread(target=CamThread)
thread.start()
app.MainLoop()
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
Friday, January 13, 2012
Using Python and wxPython to Display A Motion JPEG From the TRENDnet Wireless Internet Camera
Posted by
MrLowerr
Sunday, November 6, 2011
9DOF IMU Development For Dynamixel Based Robots - Part 1
I recently purchased the 9 Degrees of Freedom - Razor IMU from Sparkfun Electronics. My intentions with this device is to use it as a platform to start the development of an IMU that communicates on the Dynamixel AX (and/or RX /EX) buss for easy use in Dynamixel based robots.
Posted by
MrLowerr
Sunday, March 27, 2011
Python Hermite Curve Calculation And Display
A Hermite curve defines a unique path between two points based off of two control tangents.
I put together a little python program to play around with the calculation of Hermite curves to make sure I understood them before attempting to use it in other applications.
I put together a little python program to play around with the calculation of Hermite curves to make sure I understood them before attempting to use it in other applications.
Posted by
MrLowerr
Sunday, March 13, 2011
Logitech Dual Action USB Gamepad Interface Using Python
You can get a list of all input device files on your machine by using the command 'ls /dev/input'. My Logitech gamepad is showing up as 'js0'.
Using the cat command on the gamepad's device file 'cat /dev/input/js0' and then pressing some buttons on the game pad spews out a bunch of garbage in the terminal. This lets me know the gamepad is working but I am going to have to do a bit of programming to make some sense of all this gibberish.
Using python we can open and read the device pipe as if it were a normal file and print it in a slightly more readable format.
By observing the output of the simple python program I notice that each event on the gamepad (button down, button up, axis movement) produces 8 bytes of data. I then went on to modify the program to assemble the incoming data into eight byte messages and print each message to the screen.
By observing the data in this format I am able to determine a few things:
From here I am just a hop, skip, and a jump away from wrapping this into a class and saving the state of the buttons and axis into some sort of easily used data structures. I will be posting a follow up post when I get this concept into a usable format.
Using the cat command on the gamepad's device file 'cat /dev/input/js0' and then pressing some buttons on the game pad spews out a bunch of garbage in the terminal. This lets me know the gamepad is working but I am going to have to do a bit of programming to make some sense of all this gibberish.
Using python we can open and read the device pipe as if it were a normal file and print it in a slightly more readable format.
import sys
# Open the js0 device as if it were a file in read mode.
pipe = open('/dev/input/js0', 'r')
# Loop forever.
while 1:
# For each character read from the /dev/input/js0 pipe...
for char in pipe.read(1):
# write to the standard output the string representation of 'char'.
sys.stdout.write(repr(char))
# Flush the stdout pipe.
sys.stdout.flush()
By observing the output of the simple python program I notice that each event on the gamepad (button down, button up, axis movement) produces 8 bytes of data. I then went on to modify the program to assemble the incoming data into eight byte messages and print each message to the screen.
# Open the js0 device as if it were a file in read mode.
pipe = open('/dev/input/js0', 'r')
# Create an empty list to store read characters.
msg = []
# Loop forever.
while 1:
# For each character read from the /dev/input/js0 pipe...
for char in pipe.read(1):
# append the integer representation of the unicode character read to the msg list.
msg += [ord(char)]
# If the length of the msg list is 8...
if len(msg) == 8:
# Print the msg list.
print msg
# Reset msg as an empty list.
msg = []
By observing the data in this format I am able to determine a few things:
- Bytes 0, 1, 2,and 3 appear to count up as messages stream in. This could possibly be useful to determine the order or time relationship between messages.
- Bytes 4 and 5 are the value of the message.
- Byte 6 seems to serves two functions.
- When the device pipe is first opened we receive messages that indicate how many buttons and how many joystick axis are on the gamepad. If the byte is 129 this represents a button or if it is 130 it represent a joystick axis.
- After these initial messages if the byte is 1 it represents a button event or if its 2 it represents a axis event.
- Byte 7 identifies the button or axis number that triggered the message.
# Open the js0 device as if it were a file in read mode.
pipe = open('/dev/input/js0', 'r')
# Create an empty list to store read characters.
msg = []
# Loop forever.
while 1:
# For each character read from the /dev/input/js0 pipe...
for char in pipe.read(1):
# append the integer representation of the unicode character read to the msg list.
msg += [ord(char)]
# If the length of the msg list is 8...
if len(msg) == 8:
# Button event if 6th byte is 1
if msg[6] == 1:
if msg[4] == 1:
print 'button', msg[7], 'down'
else:
print 'button', msg[7], 'up'
# Axis event if 6th byte is 2
elif msg[6] == 2:
print 'axis', msg[7], msg[5]
# Reset msg as an empty list.
msg = []
From here I am just a hop, skip, and a jump away from wrapping this into a class and saving the state of the buttons and axis into some sort of easily used data structures. I will be posting a follow up post when I get this concept into a usable format.
Posted by
MrLowerr
Saturday, November 20, 2010
Fractal Terrain Generation Using The Diamond-Square Algorithm
Continuing on with my experimentation with the fractal mountain range and Mandelbrot set I have set my eyes on generating a 3d fractal landscape with ambitions of moving onto a fractal planet.
I have been tinkering with the Pyglet python library. Pyglet is a wrapper for the OpenGL (Open Graphics Library).
Shown below is my attempts at visualizing the result of a terrain generated from the diamod-square algorithm
I have been tinkering with the Pyglet python library. Pyglet is a wrapper for the OpenGL (Open Graphics Library).
Shown below is my attempts at visualizing the result of a terrain generated from the diamod-square algorithm
First attempt. Has some obvious algorithm errors.
Second attempt with a revised algorithm.
Posted by
MrLowerr
Thursday, November 18, 2010
Generating A Fractal Mountain Range
I stumbled across this page about generating random fractal terrain. I went ahead and made a simple wxpython app to put the ideas I learned into practice. Below is the result and source code.
Posted by
MrLowerr
Saturday, November 13, 2010
Using Python To Draw The Mandelbrot Set
I recently watched a documentary on the discovery of the Mandelbrot Set on youtube and it sparked my curiosity. I went ahead and made a little python app that draws the famous Mandelbrot set in a wxpython window. You can see the result of my work and the source code below. It is by no means useful or elegant code but it was a fun exercise!
Posted by
MrLowerr
Monday, August 2, 2010
Gold Rush Serial Data Packet Control
Over the last week I have been able to get wireless control working with my AVR micro. Communication to the AVR is being done by sending wireless serial data packets using xbee modules.
At the moment my packets consist of two header bytes (0xff) to designate the start of a new packet. The header is then followed with 4 bytes to hold the values of 2 joystick (horizontal and vertical axis). The packet is then ended with a checksum.
I have created a simple python and wxpython app to simulate the control of two joysticks using some slider widgets. This app simply reads the value of the 4 sliders, forms a data packet and writes it out serially to a xbee module connected to my PC around 30 times a second.
On the micro side I have a second xbee module connected to one of the atmega644's usarts. The micro is adding every byte it receives on this usart to a ring buffer. Then, when called, a function is able to search this buffer for a full data packet, pull out the joystick data and flush the buffer.
Here is a little video I took to demonstrate it all working. Off camera I am changing the values on the top two sliders in my python app. These two sliders represent the vertical and horizontal axis of a single joystick. In turn the value of each slider determines the walking direction of the robot.
At the moment my packets consist of two header bytes (0xff) to designate the start of a new packet. The header is then followed with 4 bytes to hold the values of 2 joystick (horizontal and vertical axis). The packet is then ended with a checksum.
I have created a simple python and wxpython app to simulate the control of two joysticks using some slider widgets. This app simply reads the value of the 4 sliders, forms a data packet and writes it out serially to a xbee module connected to my PC around 30 times a second.
MechController.py demo window.
On the micro side I have a second xbee module connected to one of the atmega644's usarts. The micro is adding every byte it receives on this usart to a ring buffer. Then, when called, a function is able to search this buffer for a full data packet, pull out the joystick data and flush the buffer.
Here is a little video I took to demonstrate it all working. Off camera I am changing the values on the top two sliders in my python app. These two sliders represent the vertical and horizontal axis of a single joystick. In turn the value of each slider determines the walking direction of the robot.
Posted by
MrLowerr
Sunday, July 25, 2010
AVR Wireless Serial Communication
Progress continues with my AVR microcontroller board...
Today I was able to get some simple wireless serial communication tests working.
I followed this great tutorial post on AVR Freaks on creating interrupt driven USART echo program. I then took it a step farther and had each side of the serial link (PC and micro) echo the last character received pulse one.
All in all the test were not very interesting but they do lay groundwork for the development of a control packet structure for driving Gold Rush around.
Today I was able to get some simple wireless serial communication tests working.
I followed this great tutorial post on AVR Freaks on creating interrupt driven USART echo program. I then took it a step farther and had each side of the serial link (PC and micro) echo the last character received pulse one.
All in all the test were not very interesting but they do lay groundwork for the development of a control packet structure for driving Gold Rush around.
Posted by
MrLowerr
Friday, July 2, 2010
IPMechCam Progress
Work continues on the on IPMechCam.
I have been trickling some smaller updates in the IPMechCam thread in the Trossen Robotics Community forums. This post will serve as a wrap up of all the major updates.
IPMechCam has been rolled into a larger software package I am working on for walking robots by the name of UpgraydMech. As of today the only Upgrayd Mech tool available for testing is IPMechCam however.
Four screen widgets are available with a few more in the works. A text widget allows the display of a label along with a value as text. A graph widget displays a value over time in an XY graph. A color scale widget presents a value as a graphical representation similar to a temperature scale. And the simple cross hair widget for gun targeting.
Widget preference settings have had the most improvements. The widgets preferences dialog allows for the creation individual widget instances. It also provides a nice interface for adjusting all the properties of the widgets, managing widget instances, and soon allow the saving and loading of widget settings.
Widgets are able to pull values out of a data stream. This data stream can be accessed by other UpgraydMech tools, robot controllers and robot systems. (Arbotix support? MechWarfare scoring system?)
If you would like to try IPMechCam:
I have been trickling some smaller updates in the IPMechCam thread in the Trossen Robotics Community forums. This post will serve as a wrap up of all the major updates.
Testing IPMechCam text widget and widget preferences.
IPMechCam has been rolled into a larger software package I am working on for walking robots by the name of UpgraydMech. As of today the only Upgrayd Mech tool available for testing is IPMechCam however.
Four screen widgets are available with a few more in the works. A text widget allows the display of a label along with a value as text. A graph widget displays a value over time in an XY graph. A color scale widget presents a value as a graphical representation similar to a temperature scale. And the simple cross hair widget for gun targeting.
Widget preference settings have had the most improvements. The widgets preferences dialog allows for the creation individual widget instances. It also provides a nice interface for adjusting all the properties of the widgets, managing widget instances, and soon allow the saving and loading of widget settings.
Widgets are able to pull values out of a data stream. This data stream can be accessed by other UpgraydMech tools, robot controllers and robot systems. (Arbotix support? MechWarfare scoring system?)
If you would like to try IPMechCam:
- Download and install Python (either version 2.5 or 2.6 is fine. I use 2.6)
- Download and install wxPython GUI toolkit library.
- Download the current version of UpgraydMech.
- Unzip and run the UpgraydMech.py file.
Posted by
MrLowerr
Monday, May 10, 2010
Introducing IPMechCam
IPMechCam is a cross platform python program to display a motion jpeg stream from an IP camera. The intent is to provide a powerful, customizable and easy to use application for anyone to use in the Mech Warfare robotics competition.
By default IPMechCam will support the TRENDnet TV-IP100W camera with the option to expand support for other similar IP cameras.
The most interesting feature of IPMechCam will be its support for highly customizable HUD or Heads Up Display widgets. HUD widgets are able to display information in a visual manner over the top of IPMechCam's camera image. Information can include cross hairs to indicate targeting, sensor and control status relayed back from the mech and even indication of game status such as time remaining in the match and the current health of both you and your opponent.
IPMechCam is the first tool in a line of applications geared for the Mech Warfare robotic competition. Other tools planned include gait visualization and optimization, gait generation, pose/sequence editor and robot control interface.
Current progress can already be viewed in this thread of the Trossen Robotics community forums.
By default IPMechCam will support the TRENDnet TV-IP100W camera with the option to expand support for other similar IP cameras.
The most interesting feature of IPMechCam will be its support for highly customizable HUD or Heads Up Display widgets. HUD widgets are able to display information in a visual manner over the top of IPMechCam's camera image. Information can include cross hairs to indicate targeting, sensor and control status relayed back from the mech and even indication of game status such as time remaining in the match and the current health of both you and your opponent.
IPMechCam is the first tool in a line of applications geared for the Mech Warfare robotic competition. Other tools planned include gait visualization and optimization, gait generation, pose/sequence editor and robot control interface.
Current progress can already be viewed in this thread of the Trossen Robotics community forums.
Posted by
MrLowerr
Friday, February 26, 2010
Using Python and Pygame to Display A Motion JPEG From the TRENDnet Wireless Internet Camera
1: import time
2: import httplib
3: import base64
4: import StringIO
5: import pygame
6: from pygame.locals import *
7:
8: class trendnet:
9:
10: def __init__(self, ip, username='admin', password='admin'):
11:
12: self.ip = ip
13: self.username = username
14: self.password = password
15: self.base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
16:
17: def connect(self):
18:
19: h = httplib.HTTP(self.ip)
20: h.putrequest('GET','/cgi/mjpg/mjpeg.cgi')
21: h.putheader('Authorization', 'Basic %s' % self.base64string)
22: h.endheaders()
23: errcode, errmsg, headers = h.getreply()
24: self.file = h.getfile()
25:
26: def update(self, window, size, offset):
27:
28: data = self.file.readline()
29: if data[0:15] == 'Content-Length:':
30: count = int(data[16:])
31: s = self.file.read(count)
32: while s[0] != chr(0xff):
33: s = s[1:]
34:
35: p = StringIO.StringIO(s)
36:
37: try:
38: campanel = pygame.image.load(p).convert()
39: campanel = pygame.transform.scale(campanel, size)
40: window.blit(campanel, offset)
41:
42: except Exception, x:
43: print x
44:
45: p.close()
46:
47: if __name__ == '__main__':
48:
49: pygame.init()
50: screen = pygame.display.set_mode((660,500), 0, 32)
51:
52: pygame.display.set_caption('trendnet.py')
53:
54: background = pygame.Surface((660,500))
55: background.fill(pygame.Color('#E8E8E8'))
56: screen.blit(background, (0,0))
57:
58: camera = trendnet('192.168.1.103', 'admin', 'admin')
59: camera.connect()
60:
61: while True:
62:
63: camera.update(screen, (640,480), (10,10))
64: pygame.display.update()
65:
66: for event in pygame.event.get():
67: if event.type == QUIT:
68: sys.exit(0)
69:
70: time.sleep(.01)
download code
Posted by
MrLowerr
Subscribe to:
Posts (Atom)










