Comments (4)
Hi folks, I think I have solved this problem. The underlying issue is that bluetooth is a pretty tolerant protocol - it expects people with bluetooth devices to wander away and come back and find everything reconnects happily. This is fine if you're playing music or using a keyboard, but not so great if you're controlling a robot car (which I am also implementing π ). I can't find an easy way to change the timeout in cwiid
However, the command line tool hcitool
gives us a way of changing the timeout (at least on linux):
hcitool (from the man page)
<snip>
lst [value]
With no value, displays link supervision timeout for the connecβ
tion to the device with Bluetooth address bdaddr. If value is
given, sets the link supervision timeout for that connection to
value slots, or to infinite if value is 0.
So, I have devised a connection strategy:
import re
import cwiid
import subprocess
# set bluetooth timeout in ms
BLUETOOTH_TIMEOUT = 1000
def connect():
wm = cwiid.Wiimote()
# get all connected bluetooth devices
connected_devices = subprocess.check_output(("hcitool","con"))
# extract bluetooth MAC addresses
addresses = re.findall(r"(([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}))",connected_devices)
for i in addresses:
name = subprocess.check_output(("hcitool","name",i[0]))
# identify Wiimotes. You may need to change this if looking for balance boards
# we are also assuming that you only have one Wiimote attached at one time,
# or that we want to set them all to have the same timeout
if name.strip()=="Nintendo RVL-CNT-01":
subprocess.call(("hcitool","lst",i[0],str(BLUETOOTH_TIMEOUT*16/10)))
return wm
We can then implement @terminator14 's strategy of having a background thread to monitor the response from the wiimote.
I have implemented all the above for my project and everything stops within a couple of seconds of removing the batteries
Hope this helps,
Phil
from cwiid.
I am also running into this problem.
So far, the only thing I can think of is to run a separate thread with something like:
while True:
if global_vars.wiimote is not None:
try:
global_vars.wiimote.request_status()
except ValueError:
# Wiimote object is closed - don't worry about it
pass
except RuntimeError:
print "Disconnected suddenly"
stop_moving()
time.sleep(1)
Unfortunately, this is a pretty bad solution - during testing, if I unplug the batteries to my wiimote all of a sudden while the robot's motors (wheels) are running, the request_status() function won't error out for over 20 seconds. The robot could have run into all sorts of things in 20 seconds.
Is there a better way to do this?
from cwiid.
@furbrain this thread is two years old already but great to hear you found a potential solution. I'd like to try it out next time I fire up my robot. http://blog.alexellis.io/piwars-v2-0/
from cwiid.
@furbrain That worked really well, you're a life saver!
from cwiid.
Related Issues (20)
- Can't connect (invalid/unknown type)
- Python3 support HOT 13
- diffrent nunchuck issues HOT 1
- Feature Request: Raw Data Reporting in Python
- Wii drums support HOT 1
- domain dead
- [Q] suspicious chinese wiimote clone HOT 2
- What about Wii Motion Plus ? HOT 1
- Unable to control mouse whatsoever
- Unable to install cwiid onto my lego ev3dev robot HOT 3
- Absolute ir mouse doesn't work with xf86-input-libinput
- Suggestion: expose address in Python interface
- Communications from nunchuk connected to wiimote yield, "Received unexpected write report" HOT 1
- Documentation addition needed to man page for libcwiid
- Duplicate table entries on output with wm.state with expanded rpt_mode
- man page updates
- An improper locking bug on the lock wiimote->rpt_mutex HOT 1
- Download issue HOT 1
- Support HID wiimotes (Mayflash Dolphinbar)
- wminput bug
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cwiid.