Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Loading...
Searching...
No Matches
world_interface.h
1#pragma once
2
3#include "../gps/gps_util.h"
4#include "../kinematics/DiffDriveKinematics.h"
5#include "../navtypes.h"
6#include "../camera/Camera.h"
7#include "../camera/CameraParams.h"
8#include "../network/websocket/WebSocketServer.h"
9#include "data.h"
10#include "motor/base_motor.h"
11
12#include <array>
13#include <optional>
14#include <unordered_set>
15
20namespace robot {
21
22namespace types {
23class CameraHandle {
24public:
25 CameraHandle(std::shared_ptr<cam::Camera> cam) : camera(cam) {}
26private:
28};
29}
30
36enum class WorldInterface {
37 real,
38 sim2d,
39 sim3d,
40 noop
41};
42
45
46// TODO: add documentation
47const kinematics::DiffDriveKinematics& driveKinematics();
48
62 bool initOnlyMotors = false);
63
71
77void emergencyStop();
78
85
93
101
110bool hasNewCameraFrame(types::CameraID camera, uint32_t oldFrameNum);
111
120types::DataPoint<types::CameraFrame> readCamera(types::CameraID camera);
121
128std::optional<cam::CameraParams> getCameraIntrinsicParams(types::CameraID camera);
129
136std::optional<cv::Mat> getCameraExtrinsicParams(types::CameraID camera);
137
147
153bool gpsHasFix();
154
161types::DataPoint<navtypes::point_t> readGPS();
162
169types::DataPoint<double> readIMUHeading();
170
176types::DataPoint<Eigen::Quaterniond> readIMU();
177
183types::DataPoint<navtypes::pose_t> getTruePose();
184
192std::optional<navtypes::point_t> gpsToMeters(const navtypes::gpscoords_t& coord);
193
200
208
217void setMotorPos(robot::types::motorid_t motor, int32_t targetPos);
218
225void setMotorVel(robot::types::motorid_t motor, int32_t targetVel);
226
235types::DataPoint<int32_t> getMotorPos(robot::types::motorid_t motor);
236
237void setServoPos(robot::types::servoid_t servo, int32_t position);
238void setRequestedStepperTurnAngle(robot::types::stepperid_t stepper, int16_t angle);
239
240using callbackid_t = unsigned long long;
241
242callbackid_t addLimitSwitchCallback(
244 const std::function<void(
247
248void removeLimitSwitchCallback(callbackid_t id);
249
261template <unsigned long int N>
262types::DataPoint<navtypes::Vectord<N>>
264 navtypes::Vectord<N> motorPositions;
265 std::optional<types::datatime_t> timestamp;
266 for (size_t i = 0; i < motors.size(); i++) {
267 types::DataPoint<int32_t> positionDataPoint = robot::getMotorPos(motors[i]);
268 if (positionDataPoint.isValid()) {
269 if (!timestamp.has_value() || timestamp > positionDataPoint.getTime()) {
270 timestamp = positionDataPoint.getTime();
271 }
272 double position = static_cast<double>(positionDataPoint.getData());
273 position *= M_PI / 180.0 / 1000.0;
274 motorPositions(i) = position;
275 } else {
276 return {};
277 }
278 }
279 return types::DataPoint<navtypes::Vectord<N>>(timestamp.value(), motorPositions);
280}
281
282} // namespace robot
283
284namespace gps {
292robot::types::DataPoint<navtypes::gpscoords_t> readGPSCoords();
293} // namespace gps
_Tp power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
Represents the kinematics of a differential drive.
Definition DiffDriveKinematics.h:14
Represents data measured using a sensor at a given time.
Definition data.h:181
T getData() const
Get the value of this data point.
Definition data.h:248
datatime_t getTime() const
Get the time at which the measurement was taken.
Definition data.h:238
bool isValid() const
Check if this measurement is valid.
Definition data.h:228
Namespace for camera access system.
Definition Camera.cpp:13
::uint32_t uint32_t
::int32_t int32_t
::int16_t int16_t
Types for use in the world interface.
Definition data.cpp:3
indication_t
An indication enum, used to command the LED to flash different signals.
Definition data.h:49
motorid_t
The motors on the robot.
Definition data.h:57
std::vector< DataPoint< navtypes::point_t > > landmarks_t
A data structure that represents when each landmark was seen.
Definition data.h:41
uint32_t CameraID
The type of a camera id.
Definition data.h:46
Collection of functions for manipulating a motor.
Definition ArduPilotInterface.cpp:27
void world_interface_init(std::optional< std::reference_wrapper< net::websocket::SingleClientWSServer > > wsServer, bool initMotorsOnly)
Initialize the world interface.
Definition noop_world.cpp:24
DataPoint< point_t > readGPS()
Get the current position in the global map frame based on a GPS measurement.
Definition gps_common_interface.cpp:19
DataPoint< CameraFrame > readCamera(CameraID cameraID)
Read the latest frame from the given camera.
Definition real_world_interface.cpp:200
bool hasNewCameraFrame(CameraID cameraID, uint32_t oldFrameNum)
Check if a new camera frame from the specified camera is available.
Definition real_world_interface.cpp:183
bool isEmergencyStopped()
Check if the robot has been emergency stopped.
Definition noop_world.cpp:34
std::optional< cam::CameraParams > getCameraIntrinsicParams(CameraID cameraID)
Get the intrinsic params of the specified camera, if it exists.
Definition real_world_interface.cpp:223
DataPoint< pose_t > getTruePose()
Get the ground truth pose, if available.
Definition noop_world.cpp:46
DataPoint< Eigen::Quaterniond > readIMU()
Read the rover orientation from the IMU.
Definition ArduPilotInterface.cpp:28
std::unordered_set< CameraID > getCameras()
Get the IDs of the currently supported cameras.
Definition real_world_interface.cpp:179
std::shared_ptr< robot::base_motor > getMotor(robot::types::motorid_t motor)
Get a pointer to the motor object associated with the motor id.
Definition noop_world.cpp:28
types::DataPoint< navtypes::Vectord< N > > getMotorPositionsRad(const std::array< types::motorid_t, N > &motors)
Get the positions in radians of multiple motors at the same time.
Definition world_interface.h:263
void setMotorVel(robot::types::motorid_t motor, int32_t targetVel)
Sets the velocity of the given motor.
Definition noop_world.cpp:58
WorldInterface
An enum which defines the possible types of world interfaces.
Definition world_interface.h:36
std::shared_ptr< types::CameraHandle > openCamera(CameraID cameraID)
Opens Camera with id camID.
Definition real_world_interface.cpp:152
void setMotorPos(motorid_t motor, int32_t targetPos)
Set the target position of the motor.
Definition noop_world.cpp:52
void emergencyStop()
Emergency stop all motors.
Definition noop_world.cpp:32
DataPoint< double > readIMUHeading()
Read the current heading in the global map frame based on an IMU measurement.
Definition gps_common_interface.cpp:14
std::optional< cv::Mat > getCameraExtrinsicParams(CameraID cameraID)
Get the extrinsic params of the specified camera, if it exists.
Definition real_world_interface.cpp:240
bool gpsHasFix()
Check if the GPS sensor has acquired a fix.
Definition gps_common_interface.cpp:44
landmarks_t readLandmarks()
Read measurement from the CV system.
Definition noop_world.cpp:38
const WorldInterface WORLD_INTERFACE
The current world interface being used.
Definition real_world_interface.cpp:29
void setMotorPower(motorid_t motor, double normalizedPWM)
Set the PWM command of the given motor.
Definition noop_world.cpp:50
std::optional< point_t > gpsToMeters(const gpscoords_t &coord)
Convert GPS coordinates into a coordinate on the map frame.
Definition gps_common_interface.cpp:36
types::DataPoint< int32_t > getMotorPos(robot::types::motorid_t motor)
Get the last reported position of the specified motor.
Definition noop_world.cpp:54
void setIndicator(indication_t signal)
Set the robot indicator to indicate the given signal.
Definition noop_world.cpp:60
Represents a GPS coordinate in degrees.
Definition navtypes.h:10