Code help please 😭

So I’m trying to have code that toggles the flywheel between a “short and long” range mode. 0 for the flywheel code means it’s in short range mode. And 5 means long range. Right now, when I run the program, it automatically starts the flywheel at a “phasing” speed that goes fast, then slow, fast, slow back and forth. Then when I press r1, the button made to toggle it, it puts the flywheel, into a normal 100 percent mode.

What does your Devices setup look like? Are you also controlling the flywheel in the Controller section or in any other code blocks?
image

1 Like

At least 2 problems w/ the code.

  1. You need to specify initial condition for the flywheel motor(s). You’ve set the variable to 5, but the velocity starts at default, which is 40% if I recall. According to your code, the variable is 5 at start, but the velocity is not actually assigned until a button is pressed.

  2. Your button press/velocity setting code probably needs a delay in there. The code is executing MANY times before you can release the button best I can tell. Note: this might be wrong… I need to check the ‘trigger’ of ‘button pressed’ versus ‘button pressing’, if it’s a single instance/event versus something that will trigger again and again for a single press.

The ‘phasing’/oscillating could simply be the motor(s) having a hard time holding a default/40% velocity… a common occurrence from what I’ve seen.

1 Like

This is because the internal PID loop of the motor which is trying to maintain 75% velocity isn’t tuned for such a high inertia load. You’ll likely want to implement your own feedback controller which uses voltage control.

4 Likes

How do I do those things? Sorry I’m a beginner as of this year.

Save the pid for down the road.

  1. In your “when started” block add the following:
    set flywheel velocity to 100% (this is what level 5, your initial level, corresponds to)

  2. Combine your “when started” blocks into one. No need to split into two pieces.

  3. To make the code “cleaner” and better formatted, I don’t like how you are using the error/else condition to switch between flywheel = 0/5. If 0 is a desired condition, put it as a separate ‘if’ level inside the if/else block. This also makes the code easier to read for new teammates.

  4. No reason you can’t also put the velocity settings INSIDE the first if/else block, right after you set the variable. No need to make things more complicated than needed.

Consider why these code changes are ‘recommended’ before simply copying them.

3 Likes

The “when button pressed” event block triggers once and waits until the button is released and pressed again before triggering again. You may be thinking of an alternative method of detecting button presses, which would be having a forever loop checking for the button press every single available moment, which would not wait for the button to be released unless programmed more meticulously.

You would spin the motors with volts instead of velocity for any speed under 100%, simply replacing the “set velocity” blocks with “spin at _ volts” blocks and deleting the “spin motor forward” blocks as the volts blocks already spin the motors. To calculate the volts, multiply 12 by the % of velocity you desire, ex: for 75% velocity multiply 12x0.75, equaling 9 volts. You could also remove the blocks altering the max torque and keep it at 100 in your setup menu.

1 Like

Thanks so much. I attached a pic of my final code. I took a few different ideas from this string and added it. It works amazingly!

Code runs sequentially… the voltage commands effectively overwrite the velocity commands.

1 Like

Just an FYI, velocity is irrelevant when using voltage control for motors. The set flywheel velocity blocks don’t need to be there.

1 Like