Now that I have had my questions answered, I'd like to post a C-program that interfaces with a free android bluetooth joystick app to control the ActivityBot. The app is "Joystick BT Commander" by "Kas". It can be downloaded from GooglePlay. I'm using it on a Samsung Tablet 4, but the author says it works on a number of phones and tablets.
The link to the app is here:
https://play.google.com/store/apps/details?id=org.projectproto.btjoystick
The program below is setup to drive the ActivityBot using the joystick. There are 6 soft buttons on the app that can be used for any user defined function. I use 3 of them in the program to: end the program, as an emergency stop, and to turn ping on and off. The ping function is an example that simply lights the red and green channels of an RBG Led to show distance in front of the Bot to an object (<20cm = red, etc).
I intend to add my PIXY color tracking code as a function to be turned on and off using another button. When on, that code will take over driving the bot to go to a predefined colored object. The pixy code is at post 27 in:
http://forums.parallax.com/showthread.php/155501-Pixy-for-Propeller?p=1279736&highlight=pixy#post1279736
The comments in the code below explain the hardware connections and the BT commander data format. More info is available at the links on the app page on googleplay.
I hope that you find this interesting and something you can expand on.
Tom
/* C code for use with Propeller ActivityBot and Joystick BT Commander android App by Kas -
(c) T. Montemarano 2014-08-01, MIT License
Tested with Samsung Tablet 4
2014-08-13 Deleted & from function name in cog_run statement, added 'flgping' flag to show when ping is running.
2014-08-12 Made global declaration of *cogping, increased cog stack sizes, fixed call to do_ping
2014-08-09 used 'volatile' in global declarations.
Written for BT Commander V5 - Changed protocol -- need to do data TX to android
coded BT button 6 as 'end' to terminate program
coded BT button 5 as E-stop -- moving Joystick restarts movement
coded BT button 2 to turn ping on and off
Uncomment print statements and Run with terminal to debug or see command execution.
App options setup
Joystick Props -- behavior - deselect auto return to center
constraint - box
Buttons Props -- display 6
labels -- 1, 2 ping, 3, 4, - optional. 5 - E-stop, 6 - Quit (ends program)
Advanced -- auto connect - on
Refresh Interval - 100ms (works, but I am going to check out other values)
Timeout - Off
Uses RN42 Blutooth module. I'm using Sparkfun part that has same form as X-Bee socket
Connect BT DO to pin 9 (Will be Propeller Rx), BT DI to Prop pin 8 (will be Prop Tx)
Need to pair RN42 to android BT and connect (with ABot switch in position 1)
To run ActivityBot
Load to EEPROM, switch to 2, ABot will beep (piezo setup as in Learn examples)
After the beep and a second or so, the ABot will respond to the Joystick.
Try to tap the joystick position you want, since dragging results in
a lot of data being sent to the ABot. (The app sends data when the position of the Joystick is changed)
Joystick data is x = -100 (Full Left), y=-100 (Full down), x=+100 full right, y=+100 full up.
The buttons are decoded as Ascii 'A' Button 1 lit, 'B' 1 grey, 'C' 2 lit, etc. Button code is sent when button is pressed.
The Joystick zones are setup as approx +/- 15 y = 0 speed, +/- 15 x = drive straight fwd or back.
abs(y) >15 = move fwd (JS positive), move backwards (JS negative),
Abs(x) > approx 15 = add differential to y speed to turn or pivot (x positive -turn right.
Both x and y are scaled to set max speed and turning rates in the calculations of x2 and y2.
This can be played with to get a good range
Ping)) has signal connected to pin 5 with 2kohm resistor in series
common cathode rbg led has red to pin 6 with 100 ohm resistor, green to pin 7 with 120 ohm resistor, cathode to gnd.
To do: add pixy color tracking (use BT button 3) when active pixy tracking takes control and drives aBot to defined color object.
The BT app is free on Googleplay 'joystick BT Commander". The link on the app page leads to the details
regarding the data protocols used.
*/
#include "simpletools.h"
#include "fdserial.h"
#include "abdrive.h"
#include "ping.h"
volatile char c1[9];
volatile int c0 = 1;
volatile int nbytes;
volatile int joydata;
int flgping;
int xval;
int yval;
int flgbt;
int dist;
int *cogping;
fdserial *blut;
void getbtjoy();
void do_ping();
int main()
{
freqout(4, 1000, 2000); // Speaker tone: 1 s, 2 kHz
int x2;
int y2;
int xy;
int yspd = 0;
int xspd = 0;
flgping = 0; // ping cog is not running
cog_run(getbtjoy, 220);
drive_ramp(0, 0);
while(c0)
{
if(nbytes == 8 )
{
pause(50); // try to get rid of pause
xy = joydata;
y2 = (xy & 511) - 200;
x2 = (xy >> 10) - 200;
printi("x = %d\n", x2);
printi("y = %d\n", y2);
yspd = (abs(y2) / 15) * 15; // scale the fwd & rev motion
if(y2 <0) yspd = yspd / -2; // go slower in reverse
xspd = (abs(x2) / 15) * 4; // scale the turning
if(x2<0) xspd = xspd * -1;
printi(" left right %d %d\n",yspd+xspd, yspd-xspd);
drive_ramp(yspd+xspd, yspd-xspd); // drive, turning differential added to yspd
}
if(nbytes == 3)
{
nbytes = 0;
printi(" button = %d\n\n", c1[2]);
if(c1[2]=='C') // Button 2, on turns on ping
{
cogping = cog_run(do_ping,120);
printi(" ping cog = %d\n\n", *cogping);
nbytes = 0;
flgping = 1; // set flgping -- ping cog is running
}
if(c1[2]=='D' && flgping) // Button 2, off turns off ping, don't try to stop if not running
{
cog_end(cogping);
nbytes = 0;
flgping = 0; // clear flgping -- ping cog is stopped
}
}
} // end while c0
drive_ramp(0, 0); // stop movement at end of program
// printi(" end \n\n");
if(flgping) cog_end(cogping); // if ping running when quit is pressed, turn off
} // end main
void getbtjoy()
{
// fdserial * fdserial_open(int rxpin, int txpin, int mode, int baudrate)
blut = fdserial_open(9, 8, 0, 115200);
while(c0) // continue until button 6 - quit - is pressed
{
nbytes = 0;
int i = 1;
flgbt= 1;
c1[1] = fdserial_rxChar(blut); // read first byte from blut
if(c1[1] == 2) // if STX read next 7 bytes
{
flgbt = 1;
while(flgbt)
{
i++;
c1[i] = fdserial_rxChar(blut);
if((c1[i]==3) && (i==3 || i==8)) flgbt=0;
} // end while flgbt, android string i = 2 => button, i = 7 => x,y values
if(i==8)
{
nbytes = 8;
xval = (c1[2] - 48) * 100 + (c1[3] - 48) * 10 + (c1[4] - 48);
yval = (c1[5] - 48) * 100 + (c1[6] - 48) * 10 + (c1[7] - 48);
joydata=(xval << 10) | yval;
} // end if i= 8
if(i==3)
{
nbytes = 3;
// printi(" button = %d\n\n", c1[2]);
}
if((i==3) && (c1[2]=='K' || c1[2]=='L')) // Button 6, ends program
{
c0 = 0;
//printi(" end \n\n");
}
if((i==3) && (c1[2]=='I' || c1[2]=='J'))
{ //E-stop on button 5 press, using JS restarts movement
drive_ramp(0, 0);
}
} // end if c1=2
} // end while c0
} // end getbtjoy
void do_ping()
{
while(1)
{
dist = ping_cm(5);
if(dist<=20) // turn on red LED
{
high(6);
low(7);
}
else if(dist<=36) // turn on yellow LED
{
high(6);
high(7);
}
else // turn on green LED
{
low(6);
high(7);
}
pause(50);
} // end while
}