Macros Help

Hello, I have searched the Forum and I kept on seeing that macros is something that allows the driver to push a button and then the robot will for example move it’s arm to a certain hight. I am fairly new to coding and I am using C++ in VCS. I was wondering if anyone could explain to me how to do write macros in C++. Any help will be greatly appreciated.
Thank you.

First you’ll need a sensor on whatever motion you are measuring. The encoder in the motor works, but in some cases you might want a potentiometer for more accuracy.

some pseuocode for that could be:

when button push{
while pot != desires position range{
move arm at ((pot - desired) / x) speed
}
}

EDIT: vex code >> VCS

2 Likes

could you explain what the code does, like what “pot” is

pot is an acronym for potentiometer.
My pseudocode says ‘when you push a button, go into a loop until the desired potentiometer value is reached. In the loop, power the motor proportional to the distance the arm has to go to reach the desired position’

2 Likes

do you know how i could possible do the same, but without a potentiometer?

well you need sensor data from somewhere, if you are just using a quadrature or the motor encoder you can do the same thing but with the value from the encoder from the point of the starting position.

2 Likes

so it would be something like coding an autonamas?

I’m not sure what you mean. Yes, these macros are great for autonomous but an autonomous is not sensor data. Without the sensors, you can’t tell where your arm is at any given point. I prefer potentiometers because they are precise and it doesn’t matter what starting position it is at. With quadratures you need to know your starting position so you can refer to that. (Quadrature measures displacement from the starting position)

3 Likes

Oh, ok I get it. Sorry for the confusion and all of the questions, but if I use a potentiometer, do I place it on the axle that is going to move. And can I place that sensor on a high strength axle?

As of now the potentiometer can only fit on normal axles.
Yes, whatever sensor you use has to be on the moving axle in order to measure its movement.

hmm, ok because I am using a high strength axle, would you know any other solutions. an example program would be great.

Unfortunately I don’t have any C++ examples on hand, but its usually better to make your own so you can modify it to your specific variables anyway. Maybe have a gear on some excess spot on your high strength axle that spins a normal axle below it. Keep in mind that it’ll need to be geared so that the potentiometer stays in its range of ~250 degrees (should you choose to use potentiometers)

oh yea! I can do that. Thanks for the help.

Just so you know, if you get this working and want to improve it even more, consider using a PID loop. What Deicer suggested should work fine for most purposes but consider PID if you want to optimize further.

PID might especially prove to be helpful if your arm is heavy, as the I factor can help counter gravity. D helps minimize overshooting so that could be good too.

https://www.csimn.com/CSI_pages/PIDforDummies.html

2 Likes

Also, the V5 motors have built-in encoders that can provide information on the rotation of the shaft. However, its not really accurate because there is often give/slip that the motor encoder will not recognize.

Check out this thread, which has a very similar question:

1 Like

do you have an example program that I can see with a PID loop in it

you might be able to pair that with an encoder by figuring out the error in degrees and telling the motor to go to that position. Basically, you constantly adjust the motor’s goal to account for the motor slipping

I don’t have an example PID that isn’t a mess, but it goes something like this:

double integral = 0;
double lastValue = 0;
while (true){
double error = position - targetPosition;
integral += error;

double p = pFactor * error;
double i = iFactor * integral;
double d = dFactor * (error - lastValue)/deltaTime;

double POWER_VALUE = p + i + d;

lastValue = error;

   sleep(deltaTime);
}

sleep is named task::sleep or something I don’t entirely remember for vcs

also I would recommend that this value is fed as motor voltage, as setting velocity uses the existing PID loop inside of the motors, which is unnecessary

as for tuning, start with I and D at zero, get P so that it’s pretty good, then add a bit of I, then finally add D

2 Likes