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?
At least 2 problems w/ the code.
-
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.
-
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.
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.
How do I do those things? Sorry Iâm a beginner as of this year.
Save the pid for down the road.
-
In your âwhen startedâ block add the following:
set flywheel velocity to 100% (this is what level 5, your initial level, corresponds to) -
Combine your âwhen startedâ blocks into one. No need to split into two pieces.
-
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.
-
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.
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.
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.
Just an FYI, velocity is irrelevant when using voltage control for motors. The set flywheel velocity blocks donât need to be there.