Trig in Vexcode VR and Blocks (Plus VEX VR Project Thread Now)

I’m trying to use the Pythagorean Theorem and tangent, plus the 180 angle sum triangle theorem in Vexcode VR to make the robot travel straight to any point on the field. I’m trying to get it to just go to the center for now, but I ran into an issue. The formula for finding the value of an angle in a right triangle requires tan to the -1st power, which isn’t an existing block. Can you please add it? It would help with so many projects. Thanks.

This is what I have so far.
Code

The 45 degrees would be changed to a variable that I would set beforehand after setting the hypotenuses by finding the angle with the formula:
Tan x° = opposite/adjacent = a/b = x

tan(-1st power) of x =
turn (right or left) for (y) degrees

If you have an idea how to do this without a new block being added, please reply, and also if you think of another way having trig functions would help you.

sounds like you want atan.

image

4 Likes

Thank you. It worked for certain areas, but when I move it to positive Y, it got buggy. I’ll work on it tomorrow.

Alright, I changed it, now it only messes up with positive x.

Google atan2, that’s what you really need, but you can create that by modifying the output of atan depending on the signs of x and y.

3 Likes

My code now.
Code revised

It works. I’ll post a video on this and a few projects.

Code Project 1a

2 Likes

It is completely functional! You can set coordinates for Where you want it to go. This is the final code. Teachers, you can use it to teach about a coordinate plane, and it was fun to make and play around with.

Final Code

edit: Change the top variables to set coordinates.

5 Likes

An update we’ll be pushing tonight will add “My Blocks” to VR, so you could even make this your own My Block (function) and pass parameters to make it even more useful!

7 Likes

Thanks! I’m doing that now.

I’m trying to remake this in text, and I came across an issue.
Coding issue

Looks like vr_thread wants a reference to an actual function, not a function call. If you want to start go_to in a thread with certain parameters, you could define a function that calls go_to with those parameters, and then pass that function to vr_thread:

def go_to(x, y):
    #content of go_to

def call_go_to():
    go_to(0, 0)

vr_thread(call_go_to)

Or more concise would be to define that helper function inline as a lambda:

def go_to(x, y):
    #content of go_to

vr_thread(lambda: go_to(0, 0))
4 Likes

I hope I am not badly misinterpreting, but this is what happened.
Python is annoying sometimes

Hmm… I appear to have been wrong in my previous post. I wrote a similar program and the syntax in your original post worked fine:

# Library imports
from vexcode import *

def do_some_stuff(dist1, dist2):
    drivetrain.drive_for(FORWARD, dist1, MM)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, dist2, MM)

vr_thread(do_some_stuff(200, 200))

This program ran as expected without throwing any errors.

Maybe there’s a weird non-ASCII character or something in the call to vr_thread? Have you tried deleting that line and re-writing it?

1 Like

It actually might be a glitch. I added a new line at the beginning, and the error stayed on line #17, so it isn’t a problem with syntax.

1 Like

nevermind (20 characters)

Turns out I was formatting the parameters wrong.

x_to_new_spot = 0
y_to_new_spot = 0
angle = 0
hypotenuse = 0

def go_to(go_to__x, go_to__y):
global x_to_new_spot, y_to_new_spot, angle, hypotenuse
x_to_new_spot = location.position(X, MM) - go_to__x
y_to_new_spot = location.position(Y, MM) - go_to__y
if 0 > x_to_new_spot:
drivetrain.turn_to_heading(0, DEGREES)
if -1 < x_to_new_spot:
drivetrain.turn_to_heading(180, DEGREES)
hypotenuse = math.fabs(math.sqrt(x_to_new_spot * x_to_new_spot + y_to_new_spot * y_to_new_spot))
angle = 180 - (math.atan(y_to_new_spot / x_to_new_spot) / math.pi * 180 + 90)
drivetrain.turn_for(RIGHT, angle, DEGREES)
drivetrain.drive_for(FORWARD, hypotenuse, MM)
drivetrain.turn_to_heading(0, DEGREES)

def when_started1():
global x_to_new_spot, y_to_new_spot, angle, hypotenuse
go_to(0, 0)

vr_thread(when_started1())

1 Like