My team is trying to setup our robot for the claw on our robot to open, and stay open, on pushing button 8D, and close after pushing 8D again. One button to control the claw for open and close using a toogle button control feature.
the code above does not seem to be able to get past the first condition…
below is the code version before the one above. Below works, but it is very rough…I was attempting to smooth out the code with the version above.
#pragma config(Sensor, in1, clawPOT, sensorPotentiometer)
#pragma config(Motor, port1, claw, tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/* DOC STRING
* Toggle Button Program for VRC Remote Control Operation
* - one button pressed once opens claw
* - same button pressed again closes claw
*
* "Advanced Programming in the VEX Environment" by Peter Johnson on 1 March 2008
from Northrop Grumman slide-show presentation Link: http://slideplayer.com/slide/6837271/
*/
/** TEST CONDITIONS
*
* OBSERVED/EXPECTED PROBLEM(S):
* - claw still occasionally stutters on button press
*
* PROBLEM(S) DIAGNOSIS:
* - contact bounce
*
* PROBLEM(S) SOLUTION(S):
* - add a "changing" var
* - conditionalize the action on that var
* - when a change occurs, start a timer
* - don't accept another input change for some # of milliseconds
* - can even make more robust by resetting the timer on each pulse (so it's some # of
* milliseconds from last transition
*/
//------------------------------------------------------------------------------- VARS
int claw_closed = 1;
int claw_changing = 0; // change in progress var -- is the claw changing
// states from "closed" to "open" or vice-versa
int clawPOT_openThres = 300; // clawPOT threshold value indicating open claw
int clawPOT_closeThres = 3400; // clawPOT threshold value indicating closed claw
int clawPower = 120; // default power to claw motor
//------------------------------------------------------------------------------- TOGGLE BUTTON CODE
void claw_func(){
if(vexRT[Btn8D] == 1){
if((!claw_changing) &&
(SensorValue[clawPOT] < clawPOT_openThres) || (SensorValue[clawPOT] >clawPOT_closeThres)){
// open it
if(claw_closed){
// while(SensorValue[clawPOT] < clawPOT_openThres){
motor[claw] = clawPower;
}
// close it
else{
// while(SensorValue[clawPOT] > clawPOT_closeThres){
motor[claw] = -clawPower;
}
// update status
claw_closed = !claw_closed;
claw_changing = 1;
}
else if((SensorValue[clawPOT] < clawPOT_openThres) || (SensorValue[clawPOT] > clawPOT_closeThres)){
claw_changing = 0;
}
}
}
//++++++++++++++++++++++++++++++++++++ MAIN +++++++++++++++++++++++++++++++++++++
task main()
{
while(true){
claw_func();
}
}
Are you ever stopping these motors? I don’t see you setting the motors to 0, or at least to a small value for holding position, once the claw is closed.
As for the stuttering in the second code, that looks like it should be expected. Here is why: Let’s say the claw is open. That means the pot went past the cut-off by at least a little bit. So you press the button and it starts closing. Tight away you identify the claw as closed (though it hasn’t had a chance to close yet), but you identify with claw_changing that you are in the process of closing it. A tiny moment later you’re still holding the button down, and the claw hasn’t had a chance to move outside of its threshold. So claw_changing immediately becomes 0. You’re still holding the button down a tiny moment later with the claw having only closed slightly. But you’re identifying that it is entirely closed: you’ve both switched claw_closed and set claw_changing to 0. So now the robot tries to open the claw, even though you’ve just started closing the claw. Since the motors were going the other direction a moment ago, this should essentially brake the claw, but not really close it further (assuming equal times, accelerations, etc.). Now we repeat this whole process. After a bit of this the claw will have edged far enough that when it starts closing it passes the threshold and behaves as you expect it to.
I don’t have time right now to go through the first one posted, but those issues I’ve mentioned should help a lot.
Here is a simplified version. This switches direction and maintains the same direction as long as the button is held down. When released, the button switches to activating the claw in the opposite direction. If you want it to run for time or similar instead of waiting for a release, you can put that in, as an else if between my if and else if below, also changing my else if to reflect your end condition.
I just threw it together. I also don’t code in RobotC, so it would never work for me. I see a missing } at the end of the function, though. I’ll edit that in from above.