Code for lifting the arms

So our robot has a four-bar lift. It looks more like last year’s challenges’ robot. I tried to make it so that the arm lifts only when you’re holding the button on the controller for it and stop when you lift your finger off the controller. However, it doesn’t work. Can any of you help me fix this problem? Here’s my code.

SharedScreenshot

You have to put everything into a while loop for it to work continuously. Currently the code will only run once, and will stop once the bottom is reached

Is it possible for you to paste the whole code? it will be easier to find the issue with it

4 Likes

I will add that using separate while loops for controlling arm motions will interfere with other user control motions that you may want to use. I recommend either of the following: running those functions as separate threads, or the easier and just as acceptable way is to use if-else if-else blocks to control the motion if the motors. Use one if-else if-else block for each subsystem.

5 Likes

Can you please clarify?

I think that @Cadaver_42 is suggesting that you make the entirety of arm control one function. Here is an example of that:

void armControl(){
   if(Controller1.ButtonR1.pressing()) {
   LeftArm.spin(forwad);
   RightArm.spin(reverse);
   }
   else if(Controller1.ButtonR2.pressing()) {
   LeftArm.spin(reverse);
   RightArm.spin(forward);
   } 
   else {
   LeftArm.stop()/brake();
   RightArm.stop()/brake();
   }
}

And call this entire code segment in a while loop instead of a while loop for each of the tasks. this would be done by calling it in the main task. An example of that would be:

int main() {
   while(true) {
   armControl();
   }

Furthermore, they suggest using tasks, which allow other things to happen in the background. aka they are unblocking functions, simply put. This would be done by setting it to a task rather than a function. Though I’m not sure, I think it would be done like this:

int armControl() {
   if(Controller1.ButtonR1.pressing()) {
   LeftArm.spin(forwad);
   RightArm.spin(reverse);
   }
   else if(Controller1.ButtonR2.pressing()) {
   LeftArm.spin(reverse);
   RightArm.spin(forward);
   } 
   else {
   LeftArm.stop()/brake();
   RightArm.stop()/brake();
   }

Unlike a function however, it would be called like this in the main task:

int main() {
   task myTask = task(armControl);
   while(true) {
   }

Additionally, I suggest you use macros to control the arm as it is not fun to have to constantly think about not breaking the gears on your arms. I hope this helped. Good luck!
EDIT: Typo

4 Likes

I believe this method is far more simplistic, organized, and recommended when coding as it ensures that the arm code will not obstruct the code for the other subsystems.

1 Like

Thank you all. The problem was solved with this code.
SharedScreenshot

Good to know that it works. Also as (controller name).(whatever button).pressing() is a Boolean method, so it will return either true or false, so the “==true” is redundant. It’s always good to have clean code. Anyways, good luck!

3 Likes