Go Down

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

kas

Quote
can you help me develop a code that I am doing,
to control my wheelchair by bluetooth
everything is working already
the electronic part is ok
but I'm not a programmer, it's difficult to receive the data
of the android app and transform into pwm outputs

Hi ruldi,
Sure I can help you, but there are some specific points I don't master
 - final chair integration (communication protocol)
 - safety issues

Please have a look here and here


You may contact Woody and Stephen directly, I can send you their email addresses privately
I wait for your questions

Cheers





RULDI

ola KAS

sou do Brasil, uso google tradutor

thanks for answering
I like bt comander because it constantly sends 200 in the center, this guarantees a security, if you stop sending nothing happens,
starting from the beginning, how can I send this data to port 5 and 6 of arduino, and make a simple pwm, back and forth of the engine?
already with speed control


I would like to understand how to map the sending of bt comander, from 0 to 200 and map to output pwm from 0 to 127 on the arduino port
and from 200 to 300 map to output pwm from 128 to 255 at the arduino port


a simple thing for me to understand how this mapping works

if you want I can send the code I use with a physical joystick  where I control the 2 motors of my wheelchai

#define portaH_1a  11
#define portaH_1b  10

#define portaH_2a  5
#define portaH_2b  6

#define potpin   A0
#define potpin2  A1


int val_pot_1;
int val_pot_2;

int pwm_1_a;
int pwm_1_b;

int pwm_2_a;
int pwm_2_b;

void setup()
{
  Serial.begin(9600);

  //seta os modos dos pinos
  pinMode( portaH_1a, OUTPUT );
  pinMode( portaH_2a, OUTPUT );
  pinMode( portaH_1b, OUTPUT );
  pinMode( potpin, INPUT );
  pinMode( potpin2, INPUT );
}

void loop()
{
  //leitura do potenciometro
  val_pot_1 = analogRead(potpin);
  val_pot_2 = analogRead(potpin2);

 //mapeamento para 0-511 pino A - 512-1023 pino B
  pwm_1_a = map(val_pot_1, 0, 511, 255, 0);
  pwm_1_b = map(val_pot_1, 512, 1023, 0, 255);

  //mapeamento da ponte 2
  pwm_2_a = map(val_pot_2, 0, 511, 255, 0);
  pwm_2_b = map(val_pot_2, 512, 1023, 0, 255);


  //proteçes
  if (val_pot_1 > 511)
    pwm_1_a = 0;
  else
    pwm_1_b = 0;

  if (val_pot_2 > 511)
    pwm_2_a = 0;
  else
    pwm_2_b = 0;


  //Atualiza portas da ponte H1
    if ( pwm_1_a < 8) pwm_1_a = 0;
    if ( pwm_2_a < 8) pwm_2_a = 0;
    if ( pwm_1_b < 8) pwm_1_b = 0;
    if ( pwm_2_b < 8) pwm_2_b = 0;
  analogWrite(portaH_1a, pwm_1_a);
 
  analogWrite(portaH_1b, pwm_1_b);
   

  //Atualiza portas da ponte H2
  analogWrite(portaH_2a, pwm_2_a);
 
  analogWrite(portaH_2b, pwm_2_b);
   
}

RULDI


Hi, Kas
I like bt comander because it looks a lot like my physical joystick, which I use in my wheelchair


example
0 to 200 of bt comander for 0 to 127 on arduino, exit pwm port 5
and 200 to 300 of bt comander for 128 to 255 on arduino, output pwm port 6


a simple thing for me to understand how this mapping works

if you want I can send the code that I use with a physical joystick, where I control the 2 motors of my wheelchai

I'll go for my code here, maybe it will help
in this code control 2 motors of my wheelchair

#define portaH_1a  11
#define portaH_1b  10

#define portaH_2a  5
#define portaH_2b  6

#define potpin   A0
#define potpin2  A1


int val_pot_1;
int val_pot_2;

int pwm_1_a;
int pwm_1_b;

int pwm_2_a;
int pwm_2_b;

void setup()
{
  Serial.begin(9600);

  //seta os modos dos pinos
  pinMode( portaH_1a, OUTPUT );
  pinMode( portaH_2a, OUTPUT );
  pinMode( portaH_1b, OUTPUT );
  pinMode( portaH_2b, OUTPUT );
  pinMode( potpin, INPUT );
  pinMode( potpin2, INPUT );
}

void loop()
{
  //leitura do potenciometro
  val_pot_1 = analogRead(potpin);
  val_pot_2 = analogRead(potpin2);

 //mapeamento para 0-511 pino A - 512-1023 pino B
  pwm_1_a = map(val_pot_1, 0, 511, 255, 0);
  pwm_1_b = map(val_pot_1, 512, 1023, 0, 255);

  //mapeamento da ponte 2
  pwm_2_a = map(val_pot_2, 0, 511, 255, 0);
  pwm_2_b = map(val_pot_2, 512, 1023, 0, 255);


  //proteçes
  if (val_pot_1 > 511)
    pwm_1_a = 0;
  else
    pwm_1_b = 0;

  if (val_pot_2 > 511)
    pwm_2_a = 0;
  else
    pwm_2_b = 0;


  //Atualiza portas da ponte H1
    if ( pwm_1_a < 8) pwm_1_a = 0;
    if ( pwm_2_a < 8) pwm_2_a = 0;
    if ( pwm_1_b < 8) pwm_1_b = 0;
    if ( pwm_2_b < 8) pwm_2_b = 0;
  analogWrite(portaH_1a, pwm_1_a);
 
  analogWrite(portaH_1b, pwm_1_b);
   

  //Atualiza portas da ponte H2
  analogWrite(portaH_2a, pwm_2_a);
 
  analogWrite(portaH_2b, pwm_2_b);
   
}

kas

Hi ruldi

Test this code and let me know

Code: [Select]
#define VERSION        "Controls two motors (4 PWM outputs), no buttons management"

// 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

boolean    DEBUG =         true;

#define portaH_1a  11
#define portaH_1b  10
#define portaH_2a  5
#define portaH_2b  6
int pwm_1_a;
int pwm_1_b;
int pwm_2_a;
int pwm_2_b;

#define    STX            0x02
#define    ETX            0x03
byte cmd[8] = {0, 0, 0, 0, 0, 0, 0, 0};

void setup()  {
  Serial.begin(57600);
  if(DEBUG)    Serial.println(VERSION);
 
  //seta os modos dos pinos
  pinMode( portaH_1a, OUTPUT );
  pinMode( portaH_2a, OUTPUT );
  pinMode( portaH_1b, OUTPUT );
  pinMode( portaH_2b, OUTPUT );
}

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)     setMotorSpeed(cmd);
    }
  }
}

void setMotorSpeed(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);
  if(joyX<100 || joyX>300 || joyY<100 || joyY>300)     return;      // commmunication error
  if(DEBUG)    {Serial.print(joyX); Serial.print(", "); Serial.println(joyY);}

 //mapeamento para 0-511 pino A - 512-1023 pino B
 //mapping for 0-100 pin A - 100-200 pin B
  pwm_1_a = map(joyX, 100, 200, 255, 0);
  pwm_1_b = map(joyX, 200, 300, 0, 255);

  //mapeamento da ponte 2
  pwm_2_a = map(joyY, 100, 200, 255, 0);
  pwm_2_b = map(joyY, 200, 300, 0, 255);


  //proteçes
  if (joyX > 200)     pwm_1_a = 0;
  else                pwm_1_b = 0;

  if (joyY > 200)     pwm_2_a = 0;
  else                pwm_2_b = 0;

//Deadband
    if ( pwm_1_a < 8) pwm_1_a = 0;
    if ( pwm_2_a < 8) pwm_2_a = 0;
    if ( pwm_1_b < 8) pwm_1_b = 0;
    if ( pwm_2_b < 8) pwm_2_b = 0;
   
  //Atualiza portas da ponte H1
  analogWrite(portaH_1a, pwm_1_a);
  analogWrite(portaH_1b, pwm_1_b);
 
  //Atualiza portas da ponte H2
  analogWrite(portaH_2a, pwm_2_a);
  analogWrite(portaH_2b, pwm_2_b);
}

kas

Same code with additional debug informations


RULDI


incredible worked
you are amazing
I do not know how to thank
I am very happy

I just changed from 57600 to 9600 baud
with 57600 not working, I do not know the reason
but it's perfect

if not ask too much, is it possible to configure some security?
as the motor continues to rotate when you unplug the bluetooth

thank you so much
God bless you

kas

Quote
I am very happy
Thanks for the nice words   ;)


Quote
I just changed from 57600 to 9600 baud
with 57600 not working, I do not know the reason
The reason is your Bluetooth card is configured @9600 baud
For better communication reliability, I strongly advise to set it up @57600 baud
let me know your BT card model (HC-05 or HC-06)


Quote
if not ask too much, is it possible to configure some security?
as the motor continues to rotate when you unplug the bluetooth
A bit busy for the moment, I will look at this matter by the end of the week

Cheers




RULDI


the hc06, I do not know how to configure

RULDI

the hc06, I do not know how to configure
I got it, I changed the baud to 57600
it's OK

kas

#894
May 02, 2018, 07:32 am Last Edit: May 02, 2018, 07:33 am by kas
This is Andro_four_PWM.ino RevC, with Dead Man feature (Bluetooth out of range)

This implementation stops motors if BT signal is lost and immediately restart when signal is back
For higher safety, code can be modified so that Arduino has to be rebooted to restart motion

Here is the new function
Code: [Select]
void checkDeadMan()  {                                            // stop engines if BT signal is lost
  static long previousMillis = 0;                             
  long currentMillis = millis();
  if(currentMillis - previousMillis > deadManInterval) {          // check is performed every deadManInterval ms
    previousMillis = currentMillis;
    if(deadManTimeout)  {                                         // BT signal is lost, stop motors
      pwm_1_a = 0;
      pwm_2_a = 0;
      pwm_1_b = 0;
      pwm_2_b = 0;
    }
    deadManTimeout = true;
  } 
}


Full code is attached


RULDI


Perfect
extremely safe

I'll try the chair
I soon post a video demonstrating your code running in my wheelchair
thank you

RULDI

ola Kas
now it's crashing the app
if I stay connected for a long time the bt comander gets slow and hangs
What can it be?
if restart the bluetooth connection bt comander back to normal, but a few minutes later it will lock again

kas

 - Is it specific to RevC, is RevB OK ??
 - Please disconnect all attached devices from Arduino and try again
 - What is BT commander App version number ?



RULDI

joystick bt comander v5.5
strange, just connect bluetooth bt comander starts to lock
Could it be because of this last part of code security?

kas

#899
May 03, 2018, 06:47 am Last Edit: May 03, 2018, 08:12 am by kas
Quote
Could it be because of this last part of code security?
Please try again Andro_four_PWM_B.ino  and let me know

Also confirm that no devices (motor driver, ...) are attached to Arduino
Did it work before ?, did you change something in your Arduino setup ?
did you change something in your smartphonr setup ?
Give me more info, I need to reproduce this situation

EDIT: please confirm your phone brand, model and android version


Go Up