Go Down

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

kas

@Bill
thanks for the feedback

Quote
I think the joystick may still be moved slightly more to the right yet.

The version I sent you yesterday should take care of that

Quote
I also think there may be a slight problem when choosing to view the data  as bytes sent....see screenshot.  Seems to be missing the closing ")"  and possibly some data

Can see that, to be fixed in next revision

Quote
Perhaps I am mistaken, I still remain very unclear on how 1's and 0's are sent and received!

Welcome is the communication protocols Univers  ]:D ]:D ]:D

Quote
I cannot seem to get anything out of the haptic feedback options,

This option brings a vibrating feedback when moving the joystick
It is device dependant (works on my Motorola G, doesn't work on my Galaxy Tab3)
This feature is useless, to be removed in next build

Quote
Also, I do have some interest regarding the Beta testing for video.  I do not own an IP camera, but that is a simple fix  $)

Let me know when you are ready


@powermik
Quote
Just (re-)discovered your software and this thread. Is there a possibility for a second joystick?

Sorry no plan for a second joystick
As most of us, my limiting factor is time  :|
I am currently building a R/C tank with built in IP camera; my focus is embeding the video stream in the App

kovacsgellert

Nice to hear that you are working at a robot equipped with an IP Camera. I have a similar project, with the difference that my control software is a C# application, so the contorlling machine is a computer not a phone. It may be off/topic but I hope you don't mind if I put a link to my robot here: https://www.youtube.com/watch?v=E7C9-4SR41o&feature=youtube_gdata_player

kas

Quote
It may be off/topic but I hope you don't mind if I put a link to my robot here: https://www.youtube.com/watch?v=E7C9-4SR41o&feature=youtube_gdata_player

You are very welcome  :)

This his my own work in progress:


The bot is based on a Rover 5 frame
PID motor control yet to be finalised

billcat


@Bill
thanks for the feedback

you're welcome!

The version I sent you yesterday should take care of that

great!

Welcome is the communication protocols Univers  ]:D ]:D ]:D

:smiley-mr-green:

This feature is useless, to be removed in next build

got it...

Let me know when you are ready

Sure, thanks!


kas

#154
Mar 07, 2014, 12:35 pm Last Edit: Mar 07, 2014, 04:12 pm by kas Reason: 1
@wildatheart
also for billcat and others  ;)
Quote
Hello Kas.

Thank you for sharing your joystick app.  I am reading 6 bytes (decimal)  2, 1, 72, 1, 72, 3.  I understand the 2 & 3 are framing bytes.  And I also understand that 1 is the MSB and 72 is the LSB, and I believe you subtract 200 (decimal) to eliminate negative values.  But I do not understand the relationship between 173 and 0 being at the center.  Would you please tell me how these values relate to 0,0 with stick in center position.

Thank you


Joystick data is transmitted as an integer = 2 Bytes (Most Significant Byte + Less Significant Byte)
according to this data frame:
        <STX  MSB-X  LSB-X  MSB-Y  LSB-Y  ETX>


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
    sendMessage( new String(new byte[] {STX, (byte) (XvalB / 128), (byte)(XvalB % 128), (byte)(YvalB / 128), (byte)(YvalB % 128), ETX } ));


So, for Xval=0 Yval=0:
 XvalB = YvalB = 200
 XvalB / 128 = YvalB / 128 = 1
 XvalB % 128 = XvalB % 128 = 72
Data frame:   <2  1  72  1  72  3>

for Xval=100 Yval=100:
 XvalB = YvalB = 300
 XvalB / 128 = YvalB / 128 = 2
 XvalB % 128 = XvalB % 128 = 44
Data frame:   <2  2  44  2  44  3>

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

 int joyX = (data[1]<<7) + data[2];
 int joyY = (data[3]<<7) + data[4];
 joyX = joyX - 200;               // Offset to avoid
 joyY = joyY - 200;               // transmitting negative numbers


VoilĂ     :)
Should you have additional questions, let me know

EDIT:
The 6 button, 4 data field App and the video App both have a slightly different protocol
(STX & ETX values are different)

wildatheart

Thx for the explanation.  Got it. 

Joystick BT Commander is working perfectly with the LG G2 phone, the Adafruit  EZ-link 315a bluetooth, and the Parallax Quickstart demo board.

Not sure what "Data XXX" is used for.  Is there any chance the MCU can send and display a serial message in your APP?  The joystick image is displayed a little above center but that's okay - it leaves room below (in line with the displayed x,y position) for a possible sent message.


kas

#156
Mar 10, 2014, 11:14 am Last Edit: Mar 10, 2014, 12:03 pm by kas Reason: 1
Quote
Joystick BT Commander is working perfectly with the LG G2 phone, the Adafruit  EZ-link 315a bluetooth, and the Parallax Quickstart demo board.
Nice to hear, I started learning microcontrollers with Basic Stamps  :)
http://forums.parallax.com/showthread.php/103850-Yet-another-balancing-robot?daysprune=-1

Quote
Not sure what "Data XXX" is used for.  Is there any chance the MCU can send and display a serial message in your APP?
YES !!!      see reply #74
Joystick BT Commander listens to the MCU and expect a 4 Byte data frame
            <STX    button status Byte    data Byte (your choice)    ETX>

This is the relevant code snippet in the Arduino demo sketch (V10.3):
Code: [Select]
 mySerial.write(STX); mySerial.write((button<<2)+4); mySerial.write(data+4); mySerial.write(ETX); // +4: avoid 0x2 & 0x3


There is another version with 4 data bytes and 6 buttons

For those interested, contact me via PM


Quote
The joystick image is displayed a little above center but that's okay

I need that type of feedback, please let me have a screen shot of the App on your LG G2 phone



wildatheart

Kas, this is a screenshot of the LG G2.  The vertical location of the joystick is not a problem but it is a bit above center.

If I could submit a longer text string to the App in the "data" area the current location of the stick would be okay.  But my current problem is... I cannot successfully send serial data from my Quickstart to Joystick.  I guessing the problem is because the Quickstart seems only accept correct data with rx set at  9600, so I can only tx at 9600 too.  Any chance the baud rate can be selectable or otherwise changed in the App?

(Your) Nice write-up and interesting project in the Parallax forums.  I intend to submit your App (with due credit) along with my work to the forums in the near future.

kas

Quote
If I could submit a longer text string to the App in the "data" area the current location of the stick would be okay

The data field can display a positive signed Byte (0 to 127), not a text string

Quote
I guessing the problem is because the Quickstart seems only accept correct data with rx set at  9600, so I can only tx at 9600 too.  Any chance the baud rate can be selectable or otherwise changed in the App?

There isn't any Baud rate value within Joystick BT Commander code
The Baud rate setting is only for communication between BT card and microcontroller, _NOT_ between Android device and BT card
Try sending   2  4  14  3
the Data field should display 14-4=10, while keeping all buttons to the OFF position

Quote
I intend to submit your App (with due credit) along with my work to the forums in the near future.

What is your current project ??
Please give my regards to my friends from the Alternate Univers (Parallax)  ;)
and post a link here when done

juycce

Great job KAS , i test again and you was right,your app works great with Android 2.3.5,and the HC-06 also compatible (bad thing is i don't recive any data,i can only send) :D:D    so...
Based on your replay at post #146 i am trying to modify the arduino code with your permision,so that on vertical move a DC Motor, and the orizontal a Servo (for steering)...
the joyY control Vertical joystick movement    ,and "joyX" the horizontal right ?
I will post the code as soon i will get it work ... Thank you ,Congratulations again for the app,Karma up ;)


wildatheart

#160
Mar 14, 2014, 06:10 am Last Edit: Mar 14, 2014, 06:38 am by wildatheart Reason: 1
Along with all others, again.. GREAT APP!  Nice layout.  Easy to use.  And all functions work as designed.  Can even send data now.

I don't expect you to modify anything, but just a couple of observations...

1.  The condition is rare and does not seem to correlate with any certain circumstance, but it has happened that the yellow stick will lock itself in a position other than "center",  usually about the 10 o'clock or the 5 o'clock location.  It seems to lock position at about 1/3 the distance from center to the outer ring.  I've seen it happen 3 or 4 times in testing.

2.  It would be nice if your APP could receive and display a text message.  ie "X axis is clear", "Pump 3 flow rate set to  40%", "Press B1 to launch"  etc.

3.  I am experimenting with a continuous rotation servo.  I may be able to correct this in my own software but as it is - when the stick is released 3 or 4 readings are sent before the stick settles into the center position.  The affect of this is the lack of ability to stop the servo rotation at a precise point.  I've tried several different refresh interval and timeout count settings but it does not seem to help.  You'd know best, but perhaps the remedy lies in my own Propeller code.  The other option would be - upon stick release --> automatic center (100,100) without the intermediate readings.

The above are just observations - no big deal.  Joystick  is a GREAT educational opportunity to learn about Bluetooth control and most will find a way to incorporate it in their projects as is.  Thanks for sharing your work.


kas

#161
Mar 14, 2014, 10:15 am Last Edit: Mar 14, 2014, 10:22 am by kas Reason: 1
@juycce
Quote
Great job KAS , i test again and you was right,your app works great with Android 2.3.5,and the HC-06 also compatible (bad thing is i don't receive any data,i can only send) smiley-grin:D    so...

You mean data from Arduino to Smartphone ??
Provided the connection is established, it should work  :smiley-roll:
You may have to drop SoftSerial communication if you still use it
Please post your current Arduino sketch

Quote
Based on your replay at post #146 i am trying to modify the arduino code with your permision, so that on vertical move a DC Motor, and the orizontal a Servo (for steering)...
the joyY control Vertical joystick movement, and "joyX" the horizontal right ?

Right


@wildatheart
Quote
Along with all others, again.. GREAT APP!  Nice layout.  Easy to use.  And all functions work as designed.  Can even send data now.

Glad it works, thanks for the comments  :)

Quote
it has happened that the yellow stick will lock itself in a position other than "center",  usually about the 10 o'clock or the 5 o'clock location.  It seems to lock position at about 1/3 the distance from center to the outer ring.  I've seen it happen 3 or 4 times in testing.

I tried and couldn't reproduce this "feature"
What is your App version ?? (Option > About...)
When the freezing occurs,
- is the BT connection established
- what is the displayed joystick position

Quote
It would be nice if your APP could receive and display a text message.  ie "X axis is clear", "Pump 3 flow rate set to  40%", "Press B1 to launch"  etc.

I will think about it  ;)

Quote
I am experimenting with a continuous rotation servo.  I may be able to correct this in my own software but as it is - when the stick is released 3 or 4 readings are sent before the stick settles into the center position.  The affect of this is the lack of ability to stop the servo rotation at a precise point.

I understand the point
I may be wrong, but a joystick is not supposed to work that way
For my pan/tilt servo device, I just uncheck Auto return to center (Option > Joystick behavior)
I get a precise positioning, no need to return to center
Am I missing something ??

Quote
Joystick  is a GREAT educational opportunity to learn about Bluetooth control and most will find a way to incorporate it in their projects as is.  Thanks for sharing your work

Thanks, comments appreciated  :)


Still working on my new toy (see reply #152)
For the moment, I am busy tuning my PID motors (more info), while monitoring battery level (8.2V)
I just toggle buttons Up or Down; the actual values are displayed in the data fields.

wildatheart

#162
Mar 16, 2014, 07:50 am Last Edit: Mar 16, 2014, 08:19 am by wildatheart Reason: 1
Kas, thanks for giving thought to the possibility of 'text'.

Started a thread in the Parallax forum using your App and the Propeller Quickstart board as a quick and dirty demo.  We've been doing a lot with C over there and the timing of this is just right to generate added interest.  [font=Verdana]Feel free to join us.[/  I think there'd be great interest in a brief tutorial on writing Android Apps too.

http://forums.parallax.com/showthread.php/154687-EZ-Bluetooth-in-***C***-and-a-nice-App

If the asterisks mess with the link, copy and paste the entire line.  I think its okay if you just click it.

nemesis159

hi kas, you could upload the apk with 6 buttons and 4 data  XD XD XD XD

juycce

this is the code i have modified a little so i can control the speed (on Vertical) of a continuous servo ,and the normal servo on horizontal .... in case someone need it ;)  :

Code: [Select]


// Andro_Pan&Tilt      V3.1
// Arduino demo sketch for Joystick BT commander V3.X
// Controls two servo motors

// 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        >>>  -180 to +180
// Options/Options for advanced users/Refresh interval  >>>  25ms

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

#include <Servo.h>

boolean    DEBUG = false;

#define    pinServo_X     9
#define    pinServo_Y     10        //continuous servo
#define    STX            0x02
#define    ETX            0x03
#define    MAX_Y          2000      // max servo speed limit       
#define    ZERO_Y         1500      // middle servo point
#define    MIN_Y          1000      // min servo speed limit
#define    ledPin1        13
#define    ledPin2        12

int i=0;
byte cmd[6] = {0, 0, 0, 0, 0, 0};
Servo myservoX;                       // create servo object
Servo myservoY;
byte buttonStatus = 0;                // first Byte sent to Android device
byte dataByte = 0;                    // second Byte sent to Android device
long previousMillis = 0;              // will store last time Buttons status was updated
boolean setButtonFeedback = false;    // momentary buttons feedback to Android device
long interval = 1000;                 // interval between Buttons status transmission (milliseconds)


void setup()  {
  Serial.begin(9600);
  myservoX.attach(pinServo_X); 
  myservoY.attach(pinServo_Y); 
  pinMode(ledPin1, OUTPUT);     
  pinMode(ledPin2, OUTPUT);   
  if(DEBUG)  Serial.println("Stepper demo for Android BT Commander V3.X");
}

void loop() {
  if(Serial.available())  {                   // data received from smartphone
    delay(5);
    cmd[0] =  Serial.read(); 
    if(cmd[0] == STX)  { 
      i=1;     
      while(Serial.available())  {
        cmd[i] = Serial.read();
        if(cmd[i] == ETX)  {
          if(i==2 && cmd[1]>48)                break;    // Button data
          if(i==5 && cmd[1]<3 && cmd[3]<3)     break;    // Joystick data
        }
        if(i>5)   break;
        i++;
      }
      if     (i==2)   getButtonState(cmd[1]);             // 3 Bytes
      else if(i==5)   setServoPosition(cmd);                                     // 5 Bytes
      else            Serial.println("Communication error");
    }
  }  else  {
    long currentMillis = millis();
    if(setButtonFeedback == true)  {                      // allow momentary button visual effect (500 ms)
      previousMillis = currentMillis - interval*0.5;   
      setButtonFeedback = false;
    } 
  }
  delay(5);
}

void setServoPosition(byte data[5])    {
  int joyX = (data[1]<<7) + data[2];
  int joyY = (data[3]<<7) + data[4];
  joyX = joyX - 200;                     
  joyY = joyY - 200;                       
 
  joyX = map(joyX, -180, 180, 180, 0);      // (-180/+180 JBC range)
  joyY = map(joyY,  0, 423, 0, 1023);       // vertical continuous servo value

  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);}
}

void getButtonState(int bStatus)  {
  switch (bStatus) {
    case '1':
      buttonStatus |= B0001;        // Button_1: ON
      Serial.println("Button_1: ON");
      digitalWrite(ledPin1, HIGH);   // LED 1 "ON"
      break;
    case '2':
      buttonStatus &= B1110;        // Button_1: OFF
      Serial.println("Button_1: OFF");
      // your code...     
      digitalWrite(ledPin1, LOW);    // LED 1 "OFF"
      break;
     
    case '3':
      buttonStatus |= B0010;        // Button_2: ON
      DEBUG = true;
      digitalWrite(ledPin2, HIGH);   // LED 2 "ON"   
      break;
     
    case '4':
      buttonStatus &= B1101;        // Button_2: OFF
      DEBUG = false;
      Serial.println("Button_2: OFF");
      digitalWrite(ledPin2, LOW);     
      break;
  }
}

... now i am working on the hard part...control a DC motor on the vertical axis with the TB6612FNG with the PWM
                                          i will need some help

Go Up