Go Down

Topic: Joystick on Esplora help (Read 4 times) previous topic - next topic

jeffmorris

How do I write code so that when I push the joystick up and left at about 45 degrees, the robot turns left forward? Push the joystick up and right at about 45 degrees, the robot turns right forward. Push the joystick down and right at about 45 degrees, the robot turns right backward. Push the joystick down and left at about 45 degrees, the robot turns left backward.

Code: [Select]

if (joystick is up and joystick is left at about 45 degrees)
{
  robot turns left forward
}
if (joystick is up and joystick is right at about 45 degrees)
{
  robot turns right forward
}
if (joystick is down and joystick is left at about 45 degrees)
{
  robot turns left backward
}
if (joystick is down and joystick is right at about 45 degrees)
{
  robot turns right backward
}

JimboZA

Well assuming you have 2x DC motors driven through an h-bridge which allows PWM of the motor speeds.... And assuming the Esplora joystick which I've never used, works just like any other 2 axis, 2 potentiometer joystick, giving analog voltages based on position.

Read the two pots to determine the position of the stick... you might need to do some Serial.print stuff to see what voltages are produced at various positions to calibrate. Depending on the readings that will give your quadrant, which will trigger your "if"s.

Then in each "if", PWM the motors to give the desired angle. (After some messing around to find what speeds give what angle calibration, eg forward left might be left motor 1/4 speed, right motor 3/4 speed or whatever.)
No PMs for help please. Not active on this forum any more.

jeffmorris

I found out from http://arduino.cc/en/Reference/EsploraReadJoystickX that the joystick on Esplora gives readings from -512 to 512 with 0 at center position. Let's say that I move the joystick up and right at about 45 degrees. The readings could be about 256 on both axis. How do I code the if test? Something like this in BASIC:

if (Esplora.readJoystickX() >= 128 and Esplora.readJoystickX() <= 256 and Esplora.readJoystickX() => 128 and Esplora.readJoystickX() =< 256) then robotturnsrightforward()

My robot has an hexapod with 18 servos (right now, it has 12 servos and I'm waiting for 6 more servos and Lynxmotion Servo Erector Set brackets).


JimboZA

Here's the Arduino pages on if and if..else. You'll notice you don't use the word then in C compared to Basic. This page explains how to do your AND-ing in C.
No PMs for help please. Not active on this forum any more.

jeffmorris

I got the code working when I push the joystick in desired direction, waiting until the robot moves, and releasing the joystick. Now, if I want to hold the joystick in desired direction as long as I want, the robot has a pause in the walking cycle. I want to use do/while loop but how can I do that?
Code: [Select]

#include <Esplora.h>
int walkflag;
void setup()
{
  Serial1.begin(115200);
  robotstand01();
  delay(500);
}
void loop()
{
  if((Esplora.readButton(JOYSTICK_UP)==HIGH) && (Esplora.readButton(JOYSTICK_DOWN)==HIGH) && (Esplora.readButton(JOYSTICK_LEFT)==HIGH) && (Esplora.readButton(JOYSTICK_RIGHT)==HIGH))
  {
    walkflag = 0;
    robotstand01();
  } 
  if((Esplora.readButton(JOYSTICK_UP)==LOW) && (Esplora.readButton(JOYSTICK_DOWN)==HIGH) && (Esplora.readButton(JOYSTICK_LEFT)==HIGH) && (Esplora.readButton(JOYSTICK_RIGHT)==HIGH))
  {
    walkflag = 1;
    robotwalk01();
    robotwalk02();
    robotwalk03();
    robotwalk04();
    robotwalk05();
    robotwalk06();
    robotwalk07();
    robotwalk08();
    robotwalk09();
  } 
  if((Esplora.readButton(JOYSTICK_UP)==HIGH) && (Esplora.readButton(JOYSTICK_DOWN)==LOW) && (Esplora.readButton(JOYSTICK_LEFT)==HIGH) && (Esplora.readButton(JOYSTICK_RIGHT)==HIGH))
  {
    walkflag = 2;
    robotwalk01();
    robotwalk07();
    robotwalk06();
    robotwalk05();
    robotwalk04();
    robotwalk03();
    robotwalk02();
    robotwalk08();
    robotwalk09();
  } 
  if((Esplora.readButton(JOYSTICK_UP)==LOW) && (Esplora.readButton(JOYSTICK_DOWN)==HIGH) && (Esplora.readButton(JOYSTICK_LEFT)==LOW) && (Esplora.readButton(JOYSTICK_RIGHT)==HIGH))
  {
    walkflag = 3;
    robotwalk10();
    robotwalk11();
    robotwalk12();
    robotwalk13();
    robotwalk14();
    robotwalk15();
    robotwalk16();
    robotwalk17();
    robotwalk09();
  } 
  if((Esplora.readButton(JOYSTICK_UP)==LOW) && (Esplora.readButton(JOYSTICK_DOWN)==HIGH) && (Esplora.readButton(JOYSTICK_LEFT)==HIGH) && (Esplora.readButton(JOYSTICK_RIGHT)==LOW))
  {
    walkflag = 4;
    robotwalk18();
    robotwalk19();
    robotwalk20();
    robotwalk21();
    robotwalk22();
    robotwalk23();
    robotwalk24();
    robotwalk25();
    robotwalk09();
  } 
  if((Esplora.readButton(JOYSTICK_UP)==HIGH) && (Esplora.readButton(JOYSTICK_DOWN)==LOW) && (Esplora.readButton(JOYSTICK_LEFT)==LOW) && (Esplora.readButton(JOYSTICK_RIGHT)==HIGH))
  {
    walkflag = 5;
    robotwalk17();
    robotwalk16();
    robotwalk15();
    robotwalk14();
    robotwalk13();
    robotwalk12();
    robotwalk11();
    robotwalk10();
    robotwalk09();
  }
  if((Esplora.readButton(JOYSTICK_UP)==HIGH) && (Esplora.readButton(JOYSTICK_DOWN)==LOW) && (Esplora.readButton(JOYSTICK_LEFT)==HIGH) && (Esplora.readButton(JOYSTICK_RIGHT)==LOW))
  {
    walkflag = 6;
    robotwalk18();
    robotwalk24();
    robotwalk23();
    robotwalk22();
    robotwalk21();
    robotwalk20();
    robotwalk19();
    robotwalk18();
    robotwalk09();
  } 

void robotstand01()
{
  Serial1.print("#0 P1500 T500 \r ");
  Serial1.print("#1 P1500 T500 \r ");
  Serial1.print("#2 P1500 T500 \r ");
  Serial1.print("#4 P1500 T500 \r ");
  Serial1.print("#5 P1500 T500 \r ");
  Serial1.print("#6 P1500 T500 \r ");
  Serial1.print("#8 P1500 T500 \r ");
  Serial1.print("#9 P1500 T500 \r ");
  Serial1.print("#10 P1500 T500 \r ");
  Serial1.print("#16 P1500 T500 \r ");
  Serial1.print("#17 P1500 T500 \r ");
  Serial1.print("#18 P1500 T500 \r ");
  Serial1.print("#20 P1500 T500 \r ");
  Serial1.print("#21 P1500 T500 \r ");
  Serial1.print("#22 P1500 T500 \r ");
  Serial1.print("#24 P1500 T500 \r ");
  Serial1.print("#25 P1500 T500 \r ");
  Serial1.print("#26 P1500 T500 \r ");
  delay(500);
}


Go Up