Skip to content

Instantly share code, notes, and snippets.

@pebreo
Last active December 27, 2015 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pebreo/7381251 to your computer and use it in GitHub Desktop.
Save pebreo/7381251 to your computer and use it in GitHub Desktop.
Remote Tank Control using Android SL4A + Bluetooth
# Video of the code in action: http://www.youtube.com/watch?v=BtmRBxRsMk4
#! python
# -= encoding=utf-8 =-
##############################################################################
#
# uibtre.py $Rev:$
# $Id$
#
# Copyright (c) 2011, shimoda as kuri65536 _dot_ hot mail _dot_ com
# ( email address: convert _dot_ to . and joint string )
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice, this list of conditions and
# the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice, this list of conditions
# and the following disclaimer in the documentation and/or other materials provided with the
# distribution.
# - Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or
# promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##############################################################################
import sys
import time
import android
def convertChar( x, y ):
"""convert y to power => 100 to 255,
convert x to ratio of left and right.
y range
:10 to 5: => stop motor
: 5 to -2: => 100 to 255 motor
:-2 to -7: => power 255
:-7 to -10: => stop program
x range::
:-10 to -7: => left 100%, right 0%
:-7 to -4: => left 0 to 100%
:-4 to 4 : => left and right = 100%
: 4 to 7 : => right 0 to 100%
: 7 to 10: => left 0%, right 100%
"""
if y > 5.:
return 'S000S000'
power = ( 5. - y ) / 7. * 155 + 100
power = power > 0 and power or 0
power = power > 255 and 255 or power
l = ( x + 8 ) / 6.0 ### -2 to -8 => 1 to 0
r = ( 8 - x ) / 6.0 ### 2 to 8 => 1 to 0
l = l > 1 and 1 or l ### limit to -inf to 1.
r = r > 1 and 1 or r
l = l > 0 and l or 0 ### limit to 0 to 1
r = r > 0 and r or 0
if l == r == 0:
l = r = power
elif r == 1:
r = power * l
l = power
elif l == 1:
l = power * r
r = power
l = l > 99 and l or 0
r = r > 99 and r or 0
return 'F%03dF%03d' % ( l, r )
d = android.Android()
def startbluetooth():
uuid = '00001101-0000-1000-8000-00805F9B34FB'
d.dialogCreateAlert( "select BlueTooth operation" )
d.dialogSetPositiveButtonText( "server" )
d.dialogSetNeutralButtonText( "client" )
d.dialogSetNegativeButtonText( "no-BT" )
d.dialogShow()
ret = d.dialogGetResponse().result[ "which" ]
if ret == "positive":
d.bluetoothMakeDiscoverable()
d.bluetoothAccept( uuid )
return True
elif ret == "neutral":
ret = d.bluetoothConnect( uuid ).result
if not ret:
d.makeToast( "bluetooth not connected" )
sys.exit( 0 )
return True
print "skip bt setup"
return False
def main():
d.startSensingTimed( 2, 300 ) ### sense start
fBT = startbluetooth()
d.dialogCreateHorizontalProgress( "accelerometer", "Hello robo", 200 )
d.dialogShow()
while True:
time.sleep( 0.3 )
x, y, z = d.sensorsReadAccelerometer().result
print "sense: ", x, y, z
if not y:
continue
if y < -3.0:
break
d.dialogSetCurrentProgress( 100. + 10.*( y - 6 ) )
fBT and d.bluetoothWrite( convertChar( x, y ) )
fBT and d.bluetoothWrite( 'S000S000' ) ### added Stop-command
d.dialogDismiss()
if __name__ == "__main__":
main()
# vi: ft=python:et:ts=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment