go_away

Author Topic: Axon II pushbutton/servo control question  (Read 621 times)

0 Members and 1 Guest are viewing this topic.

Offline bukowskiTopic starter

  • Robot Overlord
  • ****
  • Posts: 219
  • Helpful? 0
Axon II pushbutton/servo control question
« on: March 03, 2013, 02:31:58 PM »
Hello,

I have been slowly progressing in getting the axon II to do different things. I was able to use a pot input to control a modified servo, and thought a reasonable next step would be to figure out how to use the pushbutton on the Axon. Basically, I want it to switch directions when the button is pressed.

I am using Webbotlib and Project Designer. The program compiles with 0 errors and warnings, but when upload it to the controller the servo runs in one direction only, and the switch doesnt appear to do anything.

Code:
Code: [Select]
#include "hardware.h"

bool val = true;

// Initialise the hardware
void appInitHardware(void) {
initHardware();
}
// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {


if(button.pressed()){
val = (!val);
}


if((val = true)){
servo.setSpeed(DRIVE_SPEED_MIN);
}

if((val = false)){
servo.setSpeed(DRIVE_SPEED_MAX);
}



return 0;
}

I also tried a nested loop that was changing the direction when I would press the button, but on release it would cause it to revert. This would work maybe until I held the button down for a few seconds then it would run in reverse and not change.

I know this is probably something really dumb.

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: Axon II pushbutton/servo control question
« Reply #1 on: March 03, 2013, 07:48:29 PM »
The code "if (val = true)" will assign "true" to val, and then test whether that is true (which it is.)
Use double == for testing, instead of assigning.
Also, turn on compiler warnings, because with warnings on, the compiler will tell you about this!

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: Axon II pushbutton/servo control question
« Reply #2 on: March 03, 2013, 09:25:32 PM »
This is what I'd do. Also, you need to add a button debouncer.
Quote
   if(button.pressed()){
      if(val==1)
         val=0;
      else
         val=1;

      delay_ms(100);//button debounce
      }


   if((val = true)){
      servo.setSpeed(DRIVE_SPEED_MIN);
      }

    if((val = false)){
      servo.setSpeed(DRIVE_SPEED_MAX);
      }

Also, turn on compiler warnings, because with warnings on, the compiler will tell you about this!
Not GCC . . . sometimes I have only one = sign as a typo and it usually doesn't complain. :-\

Offline bukowskiTopic starter

  • Robot Overlord
  • ****
  • Posts: 219
  • Helpful? 0
Re: Axon II pushbutton/servo control question
« Reply #3 on: March 04, 2013, 01:25:13 AM »
Ahhh, thank you kindly, gentlemen. I told you it was going to be something dumb.  ;D

Edit: Great info about adding a debouncer, I will be sure to do that.
« Last Edit: March 04, 2013, 01:27:26 AM by bukowski »

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: Axon II pushbutton/servo control question
« Reply #4 on: March 04, 2013, 11:38:48 AM »
Trust me. "gcc -Wall" will warn you about assignments in if() statements.
The way to silence that warning is to put another set of parentheses around it.

Code: [Select]
  if (a = 2) // warning with -Wall
  if ((a = 2)) // no warning even with -Wall

Offline bukowskiTopic starter

  • Robot Overlord
  • ****
  • Posts: 219
  • Helpful? 0
Re: Axon II pushbutton/servo control question
« Reply #5 on: March 04, 2013, 02:02:48 PM »
Yeah, I got a warning, it suggested that I put parenthesis around it. So I put parenthesis around it, I guess I was treating the symptom and not the illness. Live and learn!

 


Get Your Ad Here

data_list