Answers for "Catcoder mars rover solution in c++"

C++
0

Catcoder mars rover solution in c++

## Level 1 - calculate the turn radius ##
## level1 2 - calculate new position and angle
import math


## solution works for this data
WHEELBASE = 1.00
DISTANCE = 1.00
STEERINGANGLE = 30.00

#WHEELBASE = 1.75
#DISTANCE = 3.14
#STEERINGANGLE = -23.00


def calculateTurnRadius(wheelbase, steeringangle):
    return round(wheelbase / math.sin(math.radians(steeringangle)), 2)

def calculateNewDirection(wheelbase, steeringangle, distance):
    turnRadius = calculateTurnRadius(wheelbase, steeringangle)
    theta = distance / turnRadius

    #brings theta to within a 180 arc
    while theta >= math.pi * 2:
        theta -= math.pi * 2

    while theta < 0:
        theta += math.pi * 2

    # calculate theta with basic sin and cos trig
    x = turnRadius - (math.cos(theta) * turnRadius)
    y = math.sin(theta) * turnRadius
    
    x = abs(round(x, 2))
    y = round(y, 2)
    theta = math.degrees(theta)

    theta = round(theta, 2)

    return x, y, theta


print(f"Turn Radius = {calculateTurnRadius(WHEELBASE, STEERINGANGLE)}")
print(f"{calculateNewDirection(WHEELBASE, STEERINGANGLE, DISTANCE)}")

Turn Radius = 2.0
(0.24, 0.96, 28.65)

 


  [1]: https://i.stack.imgur.com/tDY2u.jpg
Posted by: Guest on May-04-2022
0

Catcoder mars rover solution in c++

#include <bits/stdc++.h>
using namespace std;
#define pi 3.1416

double deg_to_rad(double deg)
{
    double rad = deg * pi;
    rad /= 180.0;
    return rad;
}

double rad_to_deg(double rad)
{
    double deg = rad * 180.0;
    deg /= pi;
    return deg;
}

double simplify_angle(double angle)
{
    int q = angle / 180;
    double simp_angle = angle - q * 180;
    return simp_angle;
}

void solve()
{
    double wheelbase, distance, steeringangle;
    cin >> wheelbase >> distance >> steeringangle;
    if (steeringangle == 0)
    {
        printf("%.2lf %.2lf %.2lf\n", 0.0, distance, 0.0);
        return;
    }

    double turingradius = wheelbase / sin(deg_to_rad(steeringangle));
    double direction = distance / turingradius;
    direction = rad_to_deg(direction);

    double x_position = turingradius - turingradius * cos(deg_to_rad(direction));
    double y_position = turingradius * sin(deg_to_rad(direction));
    direction = simplify_angle(direction);
    if (direction < 0)
        direction = 360 + direction;
    cout << round(x_position * 100) / 100 << " " << round(y_position * 100) / 100 << " " << round(direction * 100) / 100 << endl;
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    solve();
}
Posted by: Guest on May-04-2022

Code answers related to "Catcoder mars rover solution in c++"

Browse Popular Code Answers by Language