Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions Ollie_driver.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


from bluepy import btle
# import bluetooth
#import bluetooth
import sys
import struct
import time
Expand Down Expand Up @@ -149,21 +149,21 @@ def __init__(self, deviceAddress):

# This startup sequence is also identical to the one for Ollie.
# It even uses the same unlock code.
print 'Sending antidos'
print ('Sending antidos')
self.antidos.write('011i3', withResponse=True)
print 'Sending txpower'
print ('Sending txpower')
self.txpower.write('\x0007', withResponse=True)
print 'Sending wakecpu'
print ('Sending wakecpu')
self.wakecpu.write('\x01', withResponse=True)

def getSpheroCharacteristic(self, fragment):
return self.peripheral.getCharacteristics(uuid='22bb746f' + fragment + '75542d6f726568705327')[0]

def dumpCharacteristics(self):
for s in self.peripheral.getServices():
print s
print (s)
for c in s.getCharacteristics():
print c, hex(c.handle)
print (c, hex(c.handle))

def cmd(self, did, cid, data=[], answer=True, resetTimeout=True):
# Commands are as specified in Sphero API 1.50 PDF.
Expand All @@ -178,7 +178,7 @@ def cmd(self, did, cid, data=[], answer=True, resetTimeout=True):
chk ^= 255

msg = [0xff, sop2, did, cid, seq, dlen] + data + [chk]
print 'cmd:', ' '.join([chr(c).encode('hex') for c in msg])
print ('cmd:', ' '.join([chr(c).encode('hex') for c in msg]))
# Note: withResponse is very important. Most commands won't work without it.
self.roll.write(''.join([chr(c) for c in msg]), withResponse=True)

Expand Down Expand Up @@ -240,8 +240,8 @@ def __init__(self, target_name='Sphero'):
threading.Thread.__init__(self)
self.target_name = target_name
self.bt = None
# Use "sudo hcitool lescan" to find Ollie's MAC address input it at deviceAddress =
self.deviceAddress = 'DF:79:DD:9C:B6:1D'
# Use "sudo hcitool lescan" to find Ollie's MAC address input it at deviceAddress = 'EC:61:42:58:09:41'
self.deviceAddress = 'EC:61:42:58:09:41'
self.shutdown = False
self.is_connected = False
self.mask_list = None
Expand Down Expand Up @@ -575,7 +575,7 @@ def set_data_strm(self, sample_div, sample_frames, sample_mask1, pcnt, sample_ma
self.create_mask_list(sample_mask1, sample_mask2)
self.stream_mask1 = sample_mask1
self.stream_mask2 = sample_mask2
print data
print (data)
self.send(data, response)

def set_filtered_data_strm(self, sample_div, sample_frames, pcnt, response):
Expand Down Expand Up @@ -738,7 +738,7 @@ def set_raw_motor_values(self, l_mode, l_power, r_mode, r_power, response):
brake, 0x04 - ignored.
:param power: 0-255 scalar value (units?).
"""
self.send(self.pack_cmd(REQ['CMD_RAW_MOTORS'], [l_mode, l_power, r_mode, r_power]), response)
self.send(self.pack_cmd(REQ['CMD_SET_RAW_MOTORS'], [l_mode, l_power, r_mode, r_power]), response)

def send(self, data, response):
"""
Expand Down Expand Up @@ -829,7 +829,7 @@ def recv(self, num_bytes):
data = self.raw_data_buf
while len(data) > 5:
if data[:2] == RECV['SYNC']:
print "got response packet"
print ("got response packet")
# response packet
data_length = ord(data[4])
if data_length + 5 <= len(data):
Expand Down Expand Up @@ -857,7 +857,7 @@ def recv(self, num_bytes):
IDCODE['PWR_NOTIFY']):
self._async_callback_dict[IDCODE['PWR_NOTIFY']](self.parse_pwr_notify(data_packet, data_length))
else:
print "got a packet that isn't streaming"
print ("got a packet that isn't streaming")
else:
raise RuntimeError("Bad SOF : " + self.data2hexstr(data))
self.raw_data_buf = data
Expand Down Expand Up @@ -918,8 +918,8 @@ def parse_data_strm(self, data, data_length):
for i in range((data_length - 1) / 2):
unpack = struct.unpack_from('>h', ''.join(data[5 + 2 * i:]))
output[self.mask_list[i]] = unpack[0]
print self.mask_list
print output
print (self.mask_list)
print (output)
return output


Expand Down
16 changes: 14 additions & 2 deletions Ollietest.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/python
from bluepy import btle
import struct
import time
import Ollie_driver
Expand All @@ -16,7 +15,20 @@
time.sleep(1)
ollie.set_rgb_led(0,0,255,0,False)
time.sleep(3)
ollie.set_stablization(False,'False')
while 1:
key = raw_input("what is your character")
if(key == "l"):
ollie.set_raw_motor_values(1,60,1,40,'01h')
if(key== "j"):
ollie.set_raw_motor_values(1,40,1,60,'01h')
if(key== "i"):
ollie.set_raw_motor_values(1,60,1,60,'01h')
if(key == "k"):
ollie.set_raw_motor_values(2,60,2,60,'01h')
if(key=='g'):
break
time.sleep(5)
ollie.join()
ollie.disconnect()
sys.exit(1)

29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
# SpheroOllie-python
**Sphero's Ollie**
A library of functions to be used with the Sphero Ollie!

Now even better with a python API library!
#Setup
Run the following commands in your terminal. You must be running this on a computer that has Bluetooth LE (Different from standard bluetooth)<br />
`sudo apt-get install python-pip libglib2.0-dev build-essential git`<br />
`sudo pip install bluepy`<br />
`git clone https://github.com/mkkeffeler/SpheroOllie-python`<br />
`git checkout patch_branch`<br /><br />

Use "sudo hcitool lescan" to find Ollie's MAC address
input it at "`deviceAddress =`" (line 244) in the Sphero class in Ollie_driver.py
#Getting Ready
Use `sudo hcitool lescan` to find Ollie's MAC address

**
input this device address "`deviceAddress =`" (line 244) in the Sphero class in Ollie_driver.py

Currently, Ollietest.py allows you to direct your Ollie where to go using the I J K and L keys. The G key is used to end the program.

***Included Scripts:***

***Included Scripts:***
**
**OllieTest.py**
A simple program that connects to Ollie and flashes the internal RGB LED red to green to blue. You can take it a step further and add `ollie.roll` commands to make him move using the API.

**OlliejoyDrive.py**
*requires PyGame library*

Allow you to drive Ollie with a joystick/gamepad.
Allows you to drive Ollie with a joystick/gamepad.
Shows on screen feedback of analog stick as well as speed and heading
Currently setup for a Xbox 360 controller.

Expand All @@ -38,3 +45,11 @@ Tie in the btle handleNotifcations to Sphero response API

- getting sensor info, command responses, etc. back from Ollie

#Common Errors
If `sudo hcitool lescan` throws an error involving 'input/output error' perform the following commands and try again

`sudo systemctl daemon-reload` <br></br>
`sudo hciconfig hci0 down` <br></br>
`sudo hciconfig hci0 up` <br></br>

If that doesn't work please open an issue with details.