Thunderbots Project
Loading...
Searching...
No Matches
thunderloop.h
1#pragma once
2
3#include <chrono>
4#include <csignal>
5#include <fstream>
6#include <iostream>
7#include <thread>
8
9#include "proto/tbots_software_msgs.pb.h"
10#include "shared/2021_robot_constants.h"
11#include "shared/constants.h"
12#include "software/embedded/primitive_executor.h"
13#include "software/embedded/redis/redis_client.h"
14#include "software/embedded/services/motor.h"
15#include "software/embedded/services/network/network.h"
16#include "software/embedded/services/power.h"
17#include "software/logger/logger.h"
18#include "software/world/robot_state.h"
19
21{
22 public:
53 Thunderloop(const RobotConstants_t &robot_constants, bool enable_log_merging,
54 const int loop_hz);
55
57
58 [[noreturn]] void runLoop();
59
60 // Services
61 std::unique_ptr<MotorService> motor_service_;
62 std::unique_ptr<NetworkService> network_service_;
63 std::unique_ptr<PowerService> power_service_;
64
65 // Clients
66 std::unique_ptr<RedisClient> redis_client_;
67
68 private:
69 /*
70 * The struct timespec consists of nanoseconds and seconds. If the nanoseconds
71 * are getting bigger than 1000000000 (= 1 second) the variable containing
72 * seconds has to be incremented and the nanoseconds decremented by 1000000000.
73 *
74 * @param ts timespec to modify
75 */
76 void timespecNorm(struct timespec &ts);
77
83 double getCpuTemperature();
84
89 double getMilliseconds(timespec time);
90
95 double getNanoseconds(timespec time);
96
100 void updateErrorCodes();
101
109 TbotsProto::MotorStatus pollMotorService(
110 struct timespec &poll_time, const TbotsProto::MotorControl &motor_control,
111 const struct timespec &time_since_prev_iteration);
112
120 TbotsProto::PowerStatus pollPowerService(struct timespec &poll_time);
121
125 void waitForNetworkUp();
126
127
128 // Input Msg Buffers
129 TbotsProto::World world_;
130 TbotsProto::Primitive primitive_;
131 TbotsProto::DirectControlPrimitive direct_control_;
132
133 // Output Msg Buffers
134 TbotsProto::RobotStatus robot_status_;
135 TbotsProto::JetsonStatus jetson_status_;
136 TbotsProto::NetworkStatus network_status_;
137 TbotsProto::PowerStatus power_status_;
138 std::optional<TbotsProto::MotorStatus> motor_status_;
139 TbotsProto::ThunderloopStatus thunderloop_status_;
140 TbotsProto::ChipperKickerStatus chipper_kicker_status_;
141 TbotsProto::PrimitiveExecutorStatus primitive_executor_status_;
142 TbotsProto::Timestamp time_sent_;
143
144 // Current State
145 RobotConstants_t robot_constants_;
146 Angle current_orientation_;
147 int robot_id_;
148 int channel_id_;
149 std::string network_interface_;
150 int loop_hz_;
151
152 // Calibrated power service constants
153 double kick_coeff_;
154 int kick_constant_;
155 int chip_pulse_width_;
156
157 // Primitive Executor
158 PrimitiveExecutor primitive_executor_;
159
160 // 500 millisecond timeout on receiving primitives before we stop the robots
161 const double PACKET_TIMEOUT_NS = 500.0 * NANOSECONDS_PER_MILLISECOND;
162
163 // Timeout after a failed ping request
164 const int PING_RETRY_DELAY_S = 1;
165
166 const std::string PATH_TO_RINGBUFFER_LOG = "/var/log/dmesg";
167
168 std::ifstream log_file = std::ifstream(PATH_TO_RINGBUFFER_LOG);
169};
170
171/*
172 * reads from the kernel ring buffer, likely /var/log/dmesg file, to see if the power
173 * is stable
174 *
175 * This is not defined in Thunderloop to allow it to be unit tested easily
176 *
177 * @return True if the power is stable, false otherwise
178 */
179bool isPowerStable(std::ifstream &log_file);
Definition angle.h:15
Definition primitive_executor.h:11
Definition thunderloop.h:21
Definition robot_constants.h:7