Go Down

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

kas

#300
Aug 05, 2014, 07:06 pm Last Edit: Aug 27, 2014, 11:00 am by kas Reason: 1
Quote
I revised the code that transfers the x & y data between cores (and it works).


I tried your code and got wrong results

Code: [Select]
unsigned int joyData;
int joyX, joyY;

void setup()  {
 Serial.begin(57600);
 
 // twm47099 code
 joyX = 280;    // 280-200 = 80
 joyY = 260;    // 260-200 = 60
 
 joyData = (joyX<<10) | joyY;                    // encoding
 // --------------------------------
 joyY = (joyData & 511) - 200;                   // decoding
 joyX = (joyData >> 10) - 200;
 Serial.print(joyX);  Serial.print("\t");  Serial.println(joyY);  Serial.println();
 // >> printed result:   -176 60  (not OK) <<


 
 // kas code
 joyX = 280;    // 280-200 = 80
 joyY = 260;    // 260-200 = 60

 joyData = (joyX-100 <<8) | joyY-100;              // encoding
 // --------------------------------
 joyX = (joyData >>8) -100;                        // decoding
 joyY = (joyData & 0xFF) -100;
 Serial.print(joyX);  Serial.print("\t");  Serial.print(joyY);
 // >> printed result:   80 60  (OK) <<
}

void loop()  {}


Probably due to data type implementation difference in Arduino and Propeller   :smiley-roll-blue: :smiley-roll-blue:

twm47099

Kas,
Interesting results.  It must be due to different number formats, or maybe the declaration of an unsigned integer?

Propeller C uses 32 bit integers.  I chose not to subtract 100, just to save a step.

Here's the code I ran on the Prop using both your and my methods.  I did declare joydata as an integer.

Code: [Select]

#include "simpletools.h"                      // Include simple tools
int joydata;
int joyY;
int joyX;
int x;
int y;

int main()                                    // Main function
{
  joyX = 280;       // x = 280-200 = 80
  joyY = 260;       // y = 260 - 200 =60

  //  Tom's Method
  joydata = (joyX << 10) | joyY;
  printi ("joydata = %b    %d\n", joydata, joydata);
  y = (joydata & 511) -200;
  x = (joydata >> 10) -200;
printi ("x = %d     y = %d\n\n", x, y );
  // results copied below
  // joydata = 1000110000100000100    286980
// x = 80     y = 60

  // Kas Method
  joydata = (joyX -100 << 8) | joyY - 100;
  printi ("joydata = %b    %d\n", joydata, joydata);
  y = (joydata & 0xFF) - 100;
  x = (joydata >> 8) - 100;
  printi ("x = %d     y = %d\n\n", x, y );
  // results copied below
  // joydata = 1011010010100000    46240
  // x = 80     y = 60
}


twm47099

I tried it again declaring joydata as an unsigned integer, and I got the same results I got in the above post.

Tom

kas

#303
Aug 07, 2014, 08:05 am Last Edit: Aug 07, 2014, 08:15 am by kas Reason: 1
Your binary format printout explains it all:
 // Propeller joydata = (280 << 10) | 260  =  100  01100001  00000100  =  286980    
 // Arduino joydata   = (180 <<  8 ) | 160    =            10110100   10100000   =    46240

All methods are OK using Arduino _long_ integers (32 bits)

16 bit Arduino _unsigned_ integer does overflow when shifted 10 times
as 286980  >  65535 (2^16 -1)

even 16 bit Arduino _signed_ integer does overflow when shifted 8 times
as 46240  >  32767 (2^15 - 1)

Quote
I tried it again declaring joydata as an unsigned integer, and I got the same results I got in the above post

Still more room, this is luxury  ;)

thornlv

Hi Kas

Been a while since replied - holidays et al.

Anyway have uploaded a vid on my youtube channel here http://youtu.be/9d16H1JVMLk, as you can see it works but have a few issues with it.

Only works fully only if powered by USB for both servos and arduino, I have constructed a prototype shield which enables me to power them independently of each other, I can also jump power to servos form arduino uno power input (battery, USB).

If I power them independently, then the servos are erratic or go full 180 degs lock - not ideal as I wont have USB power in the field.

Have tried some similar ways of control of which independent power works ok - example here http://youtu.be/bnlbc1swaTU.

have not played with the code to slow down movement or limit travel, need to resolve the power first.

Also as my HC06 has gone belly up, I bought another HC05 (CZ-HC-05 , gomcu) which works fine, albeit at 9600 baud. Cant seem to configure it with your HC-05 configurator - get comm error, so I have modified the sketch to 9600 - works fine apart from the power issue.

any thoughts

kas

#305
Aug 25, 2014, 06:06 pm Last Edit: Aug 25, 2014, 06:08 pm by kas Reason: 1
Hi Vic,

Quote
Only works fully only if powered by USB for both servos and arduino, I have constructed a prototype shield which enables me to power them independently of each other, I can also jump power to servos form arduino uno power input (battery, USB).

If I power them independently, then the servos are erratic or go full 180 degs lock - not ideal as I wont have USB power in the field.
Have tried some similar ways of control of which independent power works ok - example here


I don't see any reason why the two App's would behave differently  :smiley-sad-blue: :smiley-sad-blue:
I would have expected better results with separate power, as USB is 5 Volts by nature and servo's need 6 Volts
Are your servo's Analog or digital ??   the solution is probably here

Finally, please post the sketches for the two controllers


kas

from Google Play:
Quote
Awesome I use this great app to control my arduino-based tank. The developer seems very willing to provide a support and updates. For example he compiled a special version for me, when I was having some problems with the official release. Thanks
Jakub Topi?


Thanks Jakub for this nice feedback, much appreciated   :)

thornlv

#307
Aug 26, 2014, 10:41 pm Last Edit: Aug 28, 2014, 05:46 pm by thornlv Reason: 1
Hi Kas

Have been doing tests today and taking power readings, but to recap

Forget what I said about it being fine with the other app/controller, it suffers the same

When testing the setup using USB power, everything is fine - I took reading of the power at all points, have a steady 5v. Because it is only a mock setup, the servos move the gimbal head fine in each axis (pan/tilt), I have even edited your sketch to my preferred angles.

The 2 servos I am using are Futaba s3003 for pan (4.8 - 6v, 3.2/4.1kg torque) and a Goteck GS-5515MG for tilt(4.8-6v, 13.2/15kg torque), Both analog. The tilt needs a powerful servo as the s3003 just doesn't budge it at all. This setup works fine in Radio Control mode as each servo gets 6v from the RC Reciever - this is moving 6kg of camera/lens.

In mock up test, using USB power and camera/lens loaded - pan is fine. tilt is just about enough but cant go too far as it then needs a 'hand' back to zero, but at least it shows the potential i want.

Problems start when I want separate power for Arduino/BT and servos, cant use USB power in the field.
I will be using batteries, 4xAA 1.5v (6v) for Arduino/BT power and 5x 1.2V AA NiMH (6v, 2700 mAh) for the servos

In that setup, one of the servos locks at full 180, the other will be fine.

If I power the Ard/BT and servos with just the 4xAAs, no lock up, pan is fine, tilt is not locked but not enough power to move the camera/lens (see above, its ok with mock setup - no camera)

So this led me to measure the voltages at each point and discovered the voltage drops to 4.03v - bluetooth works/connects, pan works (does not need much torque, so the S3003 is probably given less torque than the specs), tilt just doesn't cut the mustard - this needs the torque (about 10kg I reckon)

Not sure what is happening here, does seem a power issue - as I said USB power fine, steady 5v at all points, battery 6v potential, but drops to just above 4v  :~

have included photos of BT module and my proto-shield (note also, this happens with breadboard tests making same circuits)

https://www.flickr.com/photos/birdincamera/15043545821/
https://www.flickr.com/photos/birdincamera/14860030248/

also, edited sketch using:

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     3              //Pan
#define    pinServo_Y     2              //Tilt
#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(9600);
 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, 125, 55);   //  << adjust to change motor direction Pan
 joyY = map(joyY, -100, 100, 75, 105);   //  << adjust to change motor direction Tilt

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


I am wondering, have I got a duff UNO - or is the HC-05 hogging power.

anyones help would be appreciated

kas

Quote
Hi kas

Since the vacation season for me is nearly over I started to finish my custom made 4WD chassis using Dagu 75:1 motors and wheels.

The parts that I will use are:
1. USB RoboClaw 2x15A Brushed DC Motor Controller
2. APM2.6 autopilot      https://code.google.com/p/ardurover/wiki/ArduRover2Setup
3. An arduino (probably the mega2560)
4. 7.2V/8Ah NI-Mh battery
5. You application for setting the modes of the APM2.6, viewing the route using the IP camera and for manual controlling the rover.

Can you help me interface your application with the arduino and the APM2.6?
What I was thinking is have the application send command to the arduino and then the arduino send R/C compatible signals to the APM2.6 autopilot. The Autopilot will send control signal to the motor controller.

My goal is to have a waypoint following rover using the apm2.6 and also be able to control the rover in manual mode using your application.

let my hear your thoughts 



Hi tolisn63

Nice project   :P

Not too familiar with the ArduPilot Mega   :smiley-roll-blue:
I suspect APM2.6 accepts only PPM (Pulsed Position Modulation) combined signal
You will need a PWM to PPM converter such as this one (6$ shipped).
Conversion could be made directly by Arduino, but requires interrupts and signal timing may become an issue, depending on other connected sensors

Keep us aware

tolisn63

Hi Kas

The apm auto pilot accepts both pwm(default setting) and ppm signals(using a jumper to invoke the mode) so there is no need for a converter.

kas

Quote
The apm auto pilot accepts both pwm(default setting) and ppm signals(using a jumper to invoke the mode)

Beyond PWM data, what do you plan to send from BT Commander to APM  ??

How do you switch APM from autopilot to manual mode ??
Please post a link to the Operator's Manual

tolisn63

#311
Aug 27, 2014, 10:04 pm Last Edit: Aug 27, 2014, 10:22 pm by tolisn63 Reason: 1
Hi Kas
please see my replies in red.

Quote
Beyond PWM data, what do you plan to send from BT Commander to APM  ??

Well, my goal is to use your app to steer the rover (via the apm) when in manual mode and also use the app to make the apm change modes.

Quote
How do you switch APM from autopilot to manual mode ??

Switching between modes is accomplished by changing pwm values on one channel. Thus when the pwm value is "A" the apm mode is "manual", when it is "B" the mode goes to "Auto" and so on.
Quote
Please post a link to the Operator's Manual

Unfortunately there is no user manual that can be downloaded, only online wikis depending on what type of vehicle you intend to use the APM on.
http://rover.ardupilot.com/wiki/table-of-contents/
http://plane.ardupilot.com/wiki/arduplane-setup/connecting-your-rc-gear/

keep the questions coming


kas

Quote
Switching between modes is accomplished by changing pwm values on one channel. Thus when the pwm
value is "A" the apm mode is "manual", when it is "B" the mode goes to "Auto" and so on.

What are the specific values for A & B ??
Can you point me to the wiki part where this is described in details

The objective is to create a third PWM output
If Button#1 == not pushed   then PWM = A  (manual mode, default)
If Button#1 == pushed           then PWM = B  (autopilot mode)
Korrect ??

woodygb

Quote
Problems start when I want separate power for Arduino/BT and servos, cant use USB power in the field.
I will be using batteries, 4xAA 1.5v (6v) for Arduino/BT power and 5x 1.2V AA NiMH (6v, 2700 mAh) for the servos

In that setup, one of the servos locks at full 180, the other will be fine.

If I power the Ard/BT and servos with just the 4xAAs, no lock up, pan is fine, tilt is not locked but not enough power to move the camera/lens (see above, its ok with mock setup - no camera)


This sounds like it might be just a simple wiring problem ... have you got both the battery pack GNDs linked?

kas

Hi Vic,

Quote
Not sure what is happening here, does seem a power issue

Yes, highly probable
Check your ground connections as suggested by woodygb

Quote
When testing the setup using USB power, everything is fine

What is your actual USB 5V source ??   mAh available ??

Quote
So this led me to measure the voltages at each point and discovered the voltage drops to 4.03v

No drop when iddle ??
Is your battery pack brand new or used ??

According to Goteck GS-5515MG datasheet (4.8 & 6V):


2200mA at free run, means 5+ Amps at full load
This may be too much for a 2700mAh NiMH battery set
Try a 4000mAh+ battery pack

Any possibility to reduce required torque through better camera balance ??


Go Up