Society of Robots - Robot Forum
Software => Software => Topic started by: bukowski 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:
#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.
-
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!
-
This is what I'd do. Also, you need to add a button debouncer.
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. :-\
-
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.
-
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.
if (a = 2) // warning with -Wall
if ((a = 2)) // no warning even with -Wall
-
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!