Help with V5 Blocks

I’m am working on a simple posistion tracking program in V5 Blocks. The only formulas used to get the coordinates are:

X = r cos(theta)
Y = r sin(theta)

The problem with this code is when my heading changes it seems to turn the robot(red circle) around a radius. Im not very experienced with coding so any help or examples will be appreciated!

The code

Capture

Video of code running

when calculating your coords, you need to use the change in position of the encoder, not the absolute position.

3 Likes

And how would i do this? Sorry i am very new to coding.

I’m not sure how you would do this using blocks, but the way it’s done in text is to have a variable that stores the previous value of the encoders, which is updated to the current value at the very end of the loop. And then the variable storing the current value is updated at the start of the loop. What that means is that when referencing the current and previous encoder variables, the previous one is always one cycle of the loop behind the current one.

With that you can just do the current position - the past position to get the change in position.

1 Like

okay thank you. I will try doing this using lists.

Here is the code now:

After this change it is having the same problem as before but resets to the origin.

I think the issue now is that you are using multiple forever loops, so instead of doing the setting and updating of the encoder values in the right order with the coordinate setting, you’re doing it all at once in parallel. Try putting it all in the same forever loop, with setting the current encoder position first, setting the coordinates second, and then updating the past encoder position last.

note that I have no experience with block coding so I might be missing something with how the code ordering works.

I did this but it continues to reset the distance to 0, the new posistion is always the origin.

You would need to add the calculated X and Y to the variable, not set it equal to them. Also, are you using two different encoders, since both x and y seem to have an encoder with the same name?

1 Like

no its the same encoder.
How do i calculate the x and y to the variable, what do you mean by this?

Instead of setting X1 to the math you did, set it to X1 = X1 + that math. Do the same for Y1. Also, odometry will not be that accurate without another perpendicular wheel to track if you get pushed sideways.

2 Likes

Here is the updated code:


The turning problem no longer exists but the robot(red dot) will only move along the X-axis. What did I do wrong?

You don’t need the Y1 or X1 variable. In the Y part, it’s nearly right, but you should set it to be Y2 = Y2 + math, not Y1 + Math. You need to do the same for X, setting X2 to X2 + math, like you did for Y.

2 Likes

Thank you! this is helping a lot there still seems to be a couple small problems but I think I can handle those.

If you still find yourself stuck I’m compelled to advise you to return to the holy texts.

If you’re having a good time, though, don’t let me interrupt :grin:

7 Likes

That defeats the whole purpose of vex and coding. The whole point of the competition is that you do it on your own and not somebody else. You’re basically asking for them to code it for you. There are plenty of threads on the forum for you to read and understand position tracking/odometry to code it yourself, including this one.

Thank you for everyones help. I am now trying to add another tracking wheel adjacent to the other for tracking side to side movement. How would I build this into my current code:

Code

I have looked at other pages to try understanding how to do this, but a still a little confused. Any resources or help will be appreciated! :smiley:

2 Likes

Since you’re approximating the robot’s motion as a line, it should be pretty simple. Just add in

x = x+(item 1 of newvalues*sin(heading))
y = y+(item 1 of newvalues*cos(heading))

but for your new values make sure you’re reading the change in the new encoder.

As for the project as a whole I should warn you that approximating the robot’s motion as a series of lines is not very likely to be successful.

2 Likes

How will this help? this is what I already have except you used the wrong formula for X?

This is how you factor in the rotation of your new encoder. Since it’s perpendicular to the original encoder sin and cosin are inverted. (I had the code wrong my bad)

2 Likes