Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
tests.cpp
Go to the documentation of this file.
1#include <chrono>
2#include <fstream>
3
4#include "gtest/gtest.h"
6#include "rclcpp/rclcpp.hpp"
7
8using testing::Eq;
9
17double round(double number, int n) {
18 number *= pow(10, n);
19 number = round(number);
20 return number / pow(10, n);
21}
22
27TEST(STEERING, steering1) {
28 auto new_inspection = std::make_unique<InspectionFunctions>(
29 M_PI / 6.0, 4.0, 0.254, 26, false, 0.5, 1.0);
30 for (unsigned int i = 0; i < 260; i++) {
31 double steering = new_inspection->calculate_steering(i);
32 EXPECT_FLOAT_EQ(
33 steering, sin(i * 2 * M_PI / new_inspection->turning_period_) * new_inspection->max_angle_);
34 }
35}
36
41TEST(TORQUE, torque1) {
42 auto new_inspection = std::make_unique<InspectionFunctions>(
43 M_PI / 6.0, 4.0, 0.254, 26, false, 0.5, 1.0);
44 double initial_velocity = 5;
45 // i represents time
46 for (unsigned int i = 0; i < 260; i++) {
47 initial_velocity += 0.1 * (new_inspection->calculate_throttle(initial_velocity));
48 }
49 EXPECT_NEAR(initial_velocity, 1, 0.01);
50}
51
56TEST(TORQUE, torque2) {
57 auto new_inspection = std::make_unique<InspectionFunctions>(
58 M_PI / 6.0, 4.0, 0.254, 26, false, 0.5, 1.0);
59 double initial_velocity = -4;
60 // i represents time
61 for (unsigned int i = 0; i < 260; i++) {
62 initial_velocity += 0.1 * (new_inspection->calculate_throttle(initial_velocity));
63 }
64 EXPECT_NEAR(initial_velocity, 1, 0.01);
65}
66
71TEST(TORQUE, torque3) {
72 auto new_inspection = std::make_unique<InspectionFunctions>(
73 M_PI / 6.0, 4.0, 0.254, 26, false, 0.5, 1.0);
74 double initial_velocity = 1.1;
75 // i represents time
76 for (unsigned int i = 0; i < 260; i++) {
77 initial_velocity += 0.1 * (new_inspection->calculate_throttle(initial_velocity));
78 }
79 EXPECT_NEAR(initial_velocity, 1, 0.01);
80}
81
86TEST(TORQUE, torque4) {
87 double max_angle = M_PI / 6.0;
88 double ideal_velocity = 10.0;
89 double turn_time = 0;
90 double wheel_radius = 0.254;
91 double gain = 0.5;
92 double stop_time = 30;
93 auto new_inspection = std::make_unique<InspectionFunctions>(
94 max_angle, turn_time, wheel_radius, stop_time, true, gain, ideal_velocity);
95
96 double current_velocity = 0;
97 double max_velocity = -1;
98 double min_velocity = 1000;
99 int count = 0;
100 // i represents time
101 for (unsigned int i = 0; i < static_cast<unsigned int>(stop_time) * 10; i++) {
102 // save the maximum and minimum velocities
103 max_velocity = current_velocity > max_velocity ? current_velocity : max_velocity;
104 min_velocity = current_velocity < min_velocity ? current_velocity : min_velocity;
105
106 // update velocity
107 current_velocity += 0.1 * (new_inspection->calculate_throttle(current_velocity));
108
109 // check goal has been reached and flip the goal
110 if (fabs(current_velocity - new_inspection->current_goal_velocity_) < 0.2) {
111 new_inspection->redefine_goal_velocity(current_velocity);
112 count += 1; // count how many times the ideal velocity changes
113 }
114 }
115 EXPECT_GT(count, 2); // test if the ideal velocity changes the right number of times
116
117
118 // Correct rounding and comparison using EXPECT_NEAR
119 double rounded_min_velocity = std::round(min_velocity * 10.0) / 10.0;
120 double rounded_max_velocity = std::round(max_velocity * 10.0) / 10.0;
121
122 EXPECT_NEAR(rounded_min_velocity, 0.0, 0.1);
123 EXPECT_NEAR(rounded_max_velocity, 9.9, 0.2); // Increased tolerance
124}
125
130TEST(CONVERSION, conversion1) {
131 auto new_inspection = std::make_unique<InspectionFunctions>(
132 M_PI / 6.0, 4.0, 0.254, 26, false, 0.5, 1.0);
133 double rpm = 15;
134 EXPECT_DOUBLE_EQ(0.39898226700590372, new_inspection->rpm_to_velocity(rpm));
135}
double round(double number, int n)
round to n decimal places
Definition tests.cpp:17
TEST(STEERING, steering1)
test steering output is sinusoidal
Definition tests.cpp:27