Go Down

Topic: Android Bluetooth joystick (Read 82869 times) previous topic - next topic

thornlv

Hi Kas

Glad you liked my photos. will look at your suggested link more tonight. Have just posted a youtube vid of the servo and gimball head controlled by RC. Have been out with it today, it's really hot today - all geared up, tripod set up, camera then camouflaged and RC fully charged, connected to cam ranger and - duh forgot my servo power pack  =(

anyway got some shots albeit from static position so not all lost

Here's the youtube vid - https://www.youtube.com/watch?v=lDpsGqUOG0I&feature=youtu.be

and here's the camranger - http://camranger.com/

Thanks for taking the time.

Vic

kas

#241
Jun 12, 2014, 07:13 pm Last Edit: Jun 12, 2014, 09:09 pm by kas Reason: 1
@john_lenfr
Quote
Ok, c'est bien l'appli avec le retour vidéo possible?

oops... désolé  :smiley-red:

The video App is not yet mainstream and will be available after Joystick BT Commander V5.0 release
Not too familiar with FPV equipment...

Video is acquired by any standalone IP camera, or any Android device with integrated camera + IP webcam App
Stream is sent via WiFi to the App through a router (for IP cameras) or directly (for Android cameras)

I use this mini router

Did you already test Joystick BT Commander V4.0 ??


Rubik's cube Robot solver                 forum.arduino.cc/index.php?topic=271827.0
Android Bluetooth joystick               forum.arduino.cc/index.php?topic=173246.0
Balancing robot for dummies           forum.arduino.cc/index.php/topic,8871.0

kas

#242
Jun 12, 2014, 09:07 pm Last Edit: Jun 17, 2014, 10:05 am by kas Reason: 1
@thornlv

I updated the Andro_Pan&Tilt code to make it compatible with Joystick BT commander V4.0,
should work right out of the box  ;)

EDIT:   now  Android BT Commander V5.x  compatible

Code: [Select]
#define VERSION     "\n\nAndro_Pan&Tilt V3.6 - @kas2014\n** Stepper demo for V5.x App **"

// Controls two servo motors

// V3.6: Android BT Commander V5.X compatible, no button data management
// V3.0: Android BT Commander V3.X compatible, no button data management
// V2.5 can receive both Byte & Integer data
// V2.0: removed SoftwareSerial

// Android BT Commander settings:
// Options/Options for advanced users/Data Range        >>>  -100 to +100
// Options/Options for advanced users/Refresh interval  >>>  50ms

//  Arduino pin #0 to TX BlueTooth module
// BT TX to be disconnected from D0 during sketch upload

#include <Servo.h>

boolean    DEBUG =         true;

#define    pinServo_X     9
#define    pinServo_Y     10
#define    STX            0x02
#define    ETX            0x03
#define    MIN_Y          45             // vertical move limitation
#define    MAX_Y          180
#define    ZERO_Y         60             // vertical offset

byte cmd[8] = {0, 0, 0, 0, 0, 0, 0, 0};
Servo myservoX;                         // create servo objects
Servo myservoY;

void setup()  {
 Serial.begin(57600);
 myservoX.attach(pinServo_X);  
 myservoY.attach(pinServo_Y);  
 if(DEBUG)    Serial.println(VERSION);
}

void loop() {
  if(Serial.available())  {                            // data received from smartphone
   delay(2);
   cmd[0] =  Serial.read();  
   if(cmd[0] == STX)  {
     int i=1;      
     while(Serial.available())  {
       delay(1);
       cmd[i] = Serial.read();
       if(cmd[i]>127 || i>7)                 break;     // Communication error
       if((cmd[i]==ETX) && (i==2 || i==7))   break;     // Button or Joystick data
       i++;
     }
     if(i==7)     setServoPosition(cmd);
   }
 }
}

void setServoPosition(byte data[8])    {
 int joyX = (data[1]-48)*100 + (data[2]-48)*10 + (data[3]-48);       // obtain the Int from the ASCII representation
 int joyY = (data[4]-48)*100 + (data[5]-48)*10 + (data[6]-48);
 joyX = joyX - 200;                                                  // Offset to avoid
 joyY = joyY - 200;                                                  // transmitting negative numbers

 if(joyX<-100 || joyX>100 || joyY<-100 || joyY>100)     return;      // commmunication error
 
 joyX = map(joyX, -100, 100, 180, 0);   //  << adjust to change motor direction
 joyY = map(joyY, -100, 100, 0, 180);   //  <<

 joyY+=ZERO_Y;
 joyY = constrain(joyY, MIN_Y, MAX_Y);
 myservoX.write(joyX);
 myservoY.write(joyY);
 if(DEBUG)    {Serial.print(joyX); Serial.print(", "); Serial.println(joyY);}
}


Tell me if it works
Rubik's cube Robot solver                 forum.arduino.cc/index.php?topic=271827.0
Android Bluetooth joystick               forum.arduino.cc/index.php?topic=173246.0
Balancing robot for dummies           forum.arduino.cc/index.php/topic,8871.0

billcat


Check your mail  8)
Please post a screenshot of the App



Hello Kas,
Thanks for your splendid enthusiasm!  I like the "billcat edition"!
But there is just one little problem....have a look at the screenshots :D
....first is original joystick bt plus v1.1a, second is joystick bt commander v4.0b(billcat edition)

kas

Quote
But there is just one little problem....have a look at the screenshots  :D


Oops... Looks pretty much the same  :smiley-red:
Seems I edited the wrong template
Let me have another try
Rubik's cube Robot solver                 forum.arduino.cc/index.php?topic=271827.0
Android Bluetooth joystick               forum.arduino.cc/index.php?topic=173246.0
Balancing robot for dummies           forum.arduino.cc/index.php/topic,8871.0

thornlv

Hi Kas. Will try it out in about 3 days. Have work shifts for the while won't get time till then. Looking forward to it and will let you know how it goes.

Have a great weekend

Cheers, Vic

kas

@billcat
Quote
But there is just one little problem....have a look at the screenshots  :D


Second try
Check your mail  ;)
Rubik's cube Robot solver                 forum.arduino.cc/index.php?topic=271827.0
Android Bluetooth joystick               forum.arduino.cc/index.php?topic=173246.0
Balancing robot for dummies           forum.arduino.cc/index.php/topic,8871.0

john_lenfr


@john_lenfr
Quote
Ok, c'est bien l'appli avec le retour vidéo possible?

oops... désolé  :smiley-red:
The video App is not yet mainstream and will be available after Joystick BT Commander V5.0 release
Not too familiar with FPV equipment...
Video is acquired by any standalone IP camera, or any Android device with integrated camera + IP webcam App
Stream is sent via WiFi to the App through a router (for IP cameras) or directly (for Android cameras)
I use this mini router
Did you already test Joystick BT Commander V4.0 ??

Ok, that's what I thought.
No I didn't test it because I'm just starting to use HC-05 modules.
I just found how to do the AT commands in Arduino Monitor
So I am at the beginning of the beginning  :smiley-mr-green:

billcat

Howdy Kas,
Great news.....second try's a charm, very nice.
many thanks!
  :D                                                               :D                                                           :D                                                       :D

joepro


Quote
Hello Kas,

First I would like to say thank you for the great piece of software you have written.  It has made implementing this type of controls into my project SO much easier.

I do have one question for you, but it would probably be better to give you a little background first...

I'm currently developing an autonomous mobile robot platform, with the UI to control it on an Android tablet.  I've developed most of the UI already for my device, and included a button that opens your Joystick BT Commander app directly from within my UI, for when the end user needs to manually control the robot.

However the Joystick BT Commnader app looks nothing like my UI design/theme, most notably you have a dark theme while my UI uses a "light" theme with a white background.

So my question for you is are you planning on implementing any sort of customization in this area into your app in the near future at all?  If not, is there some way that I can change the colors so our software would better compliment eachother?  I know I can go back and redesign my UI to match your app, but for my particular application (outdoors and/or in direct sunlight) a dark theme is a lot harder for the end user to see.

Thanks in advance for any assistance you can provide.  It is greatly appreciated.


Hi joepro
Thanks for the comments
Should be included in V5.0, to be posted within 4 weeks


Sounds good, I look forward to it.  Also, one other thing I noticed recently is that you have your app locked to Landscape.  Obviously by the way it is designed a landscape view is required.  Would it be possible for you to set it to sensorLandscape instead, that way it will automatically flip if the user turns their device upside down.

Once again, thank you for your fantastic software and the excellent support you provide.  Now if we could get all software developers to listen to their end users like this, the world would be a much better place. :-)

kas

#250
Jun 14, 2014, 07:06 pm Last Edit: Jun 14, 2014, 09:15 pm by kas Reason: 1
Quote
Would it be possible for you to set it to sensorLandscape instead, that way it will automatically flip if the user turns their device upside down

Thanks joepro for this suggestion, makes a lot of sense, will definitely be included in V5.0


Quote
However the Joystick BT Commander app looks nothing like my UI design/theme, most notably you have a dark theme while my UI uses a "light" theme with a white background.

Let me have the code snippet with your theme definition
Rubik's cube Robot solver                 forum.arduino.cc/index.php?topic=271827.0
Android Bluetooth joystick               forum.arduino.cc/index.php?topic=173246.0
Balancing robot for dummies           forum.arduino.cc/index.php/topic,8871.0

kas

Version 5.0 to be released

Early Warning

Backgroung:
Initial communication protocol was developped one year ago
At that time the App was simple (two buttons only, joystick values coded on a single Byte, no feedback communication from Arduino to Android)
Time has evolved, with added features, transmitted data amount has rapidly inflated

In some situations (RF noise environment) Arduino may receive erratic values and joystick values may be seen as button commands

Version 5.0 will feature a pure ASCII transmission protocol and definitely address this problem

This new version will be online next week
For those who want to anticipate and start modifying their code,
I will post the new communication protocol, together with the relevant demo sketch (AndroTest V2.0)
Rubik's cube Robot solver                 forum.arduino.cc/index.php?topic=271827.0
Android Bluetooth joystick               forum.arduino.cc/index.php?topic=173246.0
Balancing robot for dummies           forum.arduino.cc/index.php/topic,8871.0

kas

#252
Jun 15, 2014, 05:05 pm Last Edit: Jun 15, 2014, 06:22 pm by kas Reason: 1
New communication protocol for Joystick BT Commander V5.x

Communication from Android to Arduino                   (or to any microcontroller)

- Joystick information:
Joystick data is transmitted as ASCII data, according to this data frame:
        <STX  "Joy_X + 200"  "Joy_Y + 200"  ETX>

with STX=0x2, ETX=0x3


This is the relevant Android (Java) code snippet from Joystick Bluetooth Commander:
Code: [Select]

   final int XvalB = Xval + 200;
   final int YvalB = Yval + 200;
   if((((Xant!=Xval) || (Yant!=Yval)) || (mTimeoutCounter>=mMaxTimeoutCount && mMaxTimeoutCount>-1)))   // joystick position changed, or timeout occurred
    String toSend = String.format("%c%d%d%c", STX, XvalB, YvalB, ETX);
    sendMessage(toSend);


So, for Xval=0 Yval=0:
 XvalB = YvalB = 200
 '2' is Ascii 32 hex (decimal 50)
 '0' is Ascii 30 hex (decimal 48)
Data frame:   <0x02 0x32 0x30 0x30 0x32 0x30 0x30 0x03>

for Xval=100 Yval=100:
 XvalB = YvalB = 300
 '3' is Ascii 33 hex (decimal 51)
 '0' is Ascii 30 hex (decimal 48)
Data frame:   <0x02 0x33 0x30 0x30 0x33 0x30 0x30 0x03>

The data frame is send via BlueTooth to the arduino board and decoded.
This is the Arduino code snippet from AndroTest V2.0:
Code: [Select]

void getJoystickState(byte data[8])    {
 int joyX = (data[1]-48)*100 + (data[2]-48)*10 + (data[3]-48);       // obtain an Int from the ASCII representation
 int joyY = (data[4]-48)*100 + (data[5]-48)*10 + (data[6]-48);
 joyX = joyX - 200;                                                  // Offset to avoid
 joyY = joyY - 200;                                                  // transmitting negative numbers
}



- Button information:
Each time an android button is pressed, the following data frame is transmitted
               <STX  buttonState  ETX>

buttonState is transmitted as a Byte value and reflects the state of the pushed button
Button #1:  ON: 'A'      OFF: 'B'
Button #2: ON: 'C'      OFF: 'D'
Button #3  ...

On the Arduino side, data frame is decoded in getButtonState()
Code: [Select]
void getButtonState(int bStatus)  {
 switch (bStatus) {
// -----------------  BUTTON #1  -----------------------
   case 'A':
     buttonStatus |= B000001;        // ON
     Serial.println("\n** Button_1: ON **");
     // your code...      
     break;
   case 'B':
     buttonStatus &= B111110;        // OFF
     Serial.println("\n** Button_1: OFF **");
     // your code...      
     break;

// -----------------  BUTTON #2  -----------------------
   case 'C':
     buttonStatus |= B000010;        // ON
     Serial.println("\n** Button_2: ON **");
     // your code...      
     break;
   case 'D':
     buttonStatus &= B111101;        // OFF
     Serial.println("\n** Button_2: OFF **");
     // your code...      
     break;

   ....
}



Communication back from Arduino to Android:
(same as V4.0)

Arduino code
Code: [Select]
void sendBlueToothData()  {

 ....

   mySerial.print((char)STX);                                             // Start of Transmission
   mySerial.print(getButtonStatusString());  mySerial.print((char)0x1);   // buttons status feedback
   mySerial.print(GetdataInt1());            mySerial.print((char)0x4);   // datafield #1
   mySerial.print(GetdataFloat2());          mySerial.print((char)0x5);   // datafield #2
   mySerial.print(displayStatus);                                         // datafield #3
   mySerial.print((char)ETX);                                             // End of Transmission
 
 ....  
}



Data frame transmitted back from Arduino to Android device:  (by default, every 1000ms)
< STX   Buttons state   0X01   DataField#1   0x04   DataField#2   0x05   DataField#3   ETX >  

example: < 0X02    001011    0X01    120.00    0x04   -4500   0x05   Motor enabled   0x03 >

Button state:
This is a six character string reflecting the Arduino state for buttons position
button #1,#2,#4: ON, all others: OFF   >>>  001011
This feedback avoid any discrepancy with the Android device button state
An Arduino Reset will reinitialize the Android buttons, Two way communication is mandatory for "clean" control
Button state string is created in the getButtonStatusString() function.

Code: [Select]
String getButtonStatusString()  {
 String bStatus = "";
 for(i=0; i<6; i++)  {
   if(buttonStatus & B100000 >>i)      bStatus += "1";
   else                                bStatus += "0";
 }
 return bStatus;
}



Datafields:
Data is transmitted as ASCII characters using Serial.print()
Numbers are printed using an ASCII character for each digit
120.00   >>>   0X31, 0X32, 0X30, 0X2E, 0X30, 0X30

So  < 0x02     001011      0x01      120.00     0x04      -4500      0x05     Motor enabled     0x03 >
is actually transmitted as
    < 0x02,   0x30,0x30,0x31,0x30,0x31,0x31,   0X01,   0x31,0x32,0x30,0x2E,0x30,0x30,  
       0x04,  0x2D,0x34,0x35,0x30,0x30,   0x05,   0x4D,0x6F,0x74,0x6F,0x72,0x20,0x65,
       0x6E,0x61,0x62,0x6C,0x65,0x64,  0x03 >

without commas and spaces

Should you have additional questions, let me know  ;)

Rubik's cube Robot solver                 forum.arduino.cc/index.php?topic=271827.0
Android Bluetooth joystick               forum.arduino.cc/index.php?topic=173246.0
Balancing robot for dummies           forum.arduino.cc/index.php/topic,8871.0

tolisn63

will this protocol also be used in Total RC commander ?

kas

Quote
will this protocol also be used in Total RC commander ?

It will

Joystick BT Commander & Total RC commander share the same Bluetooth engine
Total RC commander has WiFi live video atop
Rubik's cube Robot solver                 forum.arduino.cc/index.php?topic=271827.0
Android Bluetooth joystick               forum.arduino.cc/index.php?topic=173246.0
Balancing robot for dummies           forum.arduino.cc/index.php/topic,8871.0

Go Up