NDEVR
API Documentation
MotorController.h
1#pragma once
2#include "RealSenseInterface/Headers/RealSenseOptions.h"
3#include <NDEVR/Connection.h>
4#include <NDEVR/Motor.h>
5#include <NDEVR/Scene.h>
6namespace NDEVR
7{
14 class RealSenseMotor : public Motor
15 {
16 public:
22 RealSenseMotor(const Model& model, uint04 axis_number, Connection* connection)
23 : Motor(model, connection->rawNetworkLog())
24 , m_connection(connection)
25 , m_axis_number(axis_number)
26 , m_last_update_time(Time::SystemTime())
27 {
28 Model::set<NDPO::name>("Servo " + String::ToAlphaString(axis_number));
30 }
31
34 [[nodiscard]] virtual bool supportsMoveTo() const override
35 {
36 return true;
37 }
38
41 [[nodiscard]] virtual bool supportsSetSpeed() const override
42 {
43 return true;
44 }
45
48 virtual void setLocation(const Angle<fltp08>& location) override
49 {
51 {
52 if (m_axis_number == ROLL)
53 {
55 + "," + String(location.as<DEGREES>()) + "\r\n");
56 }
57 else
58 {
60 + "," + String(location.as<DEGREES>() - 90.0) + "\r\n");
61 }
62 }
63 m_connection->waitForReadyRead(TimeSpan(0.5));
64 m_connection->rx();
66 emit onUpdated();
67 }
68
70 {
71 m_connection->tx("s" + String::ToAlphaString(m_axis_number).toLower() + "?\r\n");
72
73 m_connection->waitForReadyRead(TimeSpan(0.5));
74 String x = m_connection->rx();
75 if (x.size() > 0)
76 {
77 if (m_axis_number == ROLL)
79 else
81 m_is_valid = true;
82 emit onUpdated();
83 }
84 }
85
88 virtual void setSpeed(const Angle<fltp08>& speed) override
89 {
92 }
93
97 {
101 return range;
102 }
103
106 void update(Time time) override
107 {
108 try
109 {
110 if (!m_connection->isOpen())
111 {
112 if (m_connection->open())
113 {
114 getLocation();
115 }
116 else
117 {
119 return;
120 }
121 }
122 if(!m_is_valid)
123 getLocation();
125 {
126 TimeSpan span = time - m_last_update_time;
127 span = getMax(span, TimeSpan(0.1));
128 if (m_axis_number == ROLL)
130 else
132 m_last_update_time = time;
133 }
134 else if (time - m_last_update_time > TimeSpan(5.0))
135 {
136 getLocation();
137 m_last_update_time = time;
138 }
139 }
140 catch (...)
141 {
142
143 }
144 }
145
148 bool isValid() const
149 {
150 return m_is_valid;
151 }
155 bool m_is_valid = false;
156
157 };
158
165 {
166 public:
171 : Device(Scene(_t("Manager")), connection.log)
172 , m_connection_info(connection)
173 {
174 init();
175 }
176
177 void init()
178 {
179 m_motors[0] = nullptr;
180 m_motors[1] = nullptr;
182
183 }
184
187 void update(Time time) override
188 {
189 if (m_motors[0] == nullptr)
190 {
192 for (uint04 i = 0; i < 2; i++)
193 m_motors[i] = new RealSenseMotor(Scene(_t("Motor")), i, connection);
194 }
195 for (uint01 i = 0; i < 2; i++)
196 {
197 if (m_motors[i])
198 {
199 m_motors[i]->update(time);
200 }
201 }
202 }
203
207 {
208 Vector<3, Angle<fltp08>> head_angles(Angle<fltp08>(0));
209 for (uint01 i = 0; i < 2; i++)
210 {
211 if(m_motors[i])
212 head_angles[2 - i] = m_motors[i]->location();
213 }
214 head_angles[0] = RealSenseOptions::realsense_roll.get();
215 return head_angles;
216
217 }
218
221 [[nodiscard]] virtual TimeSpan updateInterval() const { return TimeSpan(1.0f / 30.0f); }
227 {
228 if(axis < 2)
229 return m_motors[axis];
230 return nullptr;
231 }
232
235 bool isValid() const
236 {
237 if (m_motors[0] == nullptr)
238 return false;
239 return m_motors[0]->isValid();
240 }
243 };
244}
Stores an angle in an optimized internal format with support for efficient trigonometric operations.
Definition Angle.h:83
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:54
A standard interface for all types of connections that allow transmitting and receiving of data betwe...
Definition Connection.h:316
void set(t_property_type property, const t_type &value)
Sets a property value in the database.
void startAutoUpdate(DesignObjectLookup *lookup=nullptr)
Starts automatic periodic updating of this device on a background thread.
Device(const Model &model, LogPtr log, QObject *parent=nullptr)
Constructs a Device from a Model with the given log and optional Qt parent.
void onUpdated()
Emitted after the device has been updated.
A core class that represents a node on model hierarchy.
Definition Model.h:292
Model createChild()
Creates a new child model and appends it to this model's child list.
virtual Angle< fltp08 > location() const
Returns the current angular position of the motor.
Angle< fltp08 > m_current_angle
The current angular position.
Definition Motor.h:123
Motor(const Model &model, LogPtr log=nullptr, QObject *parent=nullptr)
Constructs a Motor device.
virtual Angle< fltp08 > speed() const
Returns the current angular speed of the motor.
Definition Motor.h:56
Angle< fltp08 > m_current_speed
The current angular speed per second.
Definition Motor.h:124
void update(Time time) override
Updates all managed motors.
bool isValid() const
Returns whether the motor manager is valid and connected.
RealSenseMotor * m_motors[2]
The two servo motor controllers (tilt and pan).
ConnectionInfo m_connection_info
The connection info for the servo controller.
void init()
Initializes the motors and starts the auto-update loop.
RealSenseMotor * motor(uint01 axis) const
Returns a motor by axis index.
RealSenseMotorManager(const ConnectionInfo &connection)
Constructs the motor manager with the given connection info.
Vector< 3, Angle< fltp08 > > headAngle() const
Returns the current head orientation as pan, tilt, and roll angles.
virtual TimeSpan updateInterval() const
Returns the update interval for the motor manager (30 Hz).
Motor controller for a single servo axis on a RealSense scanning mount.
void getLocation()
Queries the current servo angle from the controller and updates the cached location.
RealSenseMotor(const Model &model, uint04 axis_number, Connection *connection)
Constructs a motor controller for a specific axis.
virtual bool supportsSetSpeed() const override
Returns whether this motor supports speed control.
virtual void setLocation(const Angle< fltp08 > &location) override
Moves the servo to the specified angle.
bool isValid() const
Returns whether the motor has been successfully initialized.
Bounds< 2, Angle< fltp08 > > speedRange() const override
Returns the supported speed range for this motor.
Time m_last_update_time
The time of the last update.
void update(Time time) override
Updates the motor position based on current speed and elapsed time.
virtual bool supportsMoveTo() const override
Returns whether this motor supports absolute positioning.
Connection * m_connection
The serial connection to the servo controller.
virtual void setSpeed(const Angle< fltp08 > &speed) override
Sets the angular velocity for continuous motion.
uint01 m_axis_number
The axis index for this motor.
bool m_is_valid
Whether the motor location is valid.
The root Model that is responsible for storing the underlying data for all Scene Models.
Definition Scene.h:52
The core String class for the NDEVR API.
Definition String.h:95
decltype(auto) getAs() const
Converts a string into an object.
Definition String.h:188
static String ToAlphaString(uint04 number)
Converts a number into a char string.
static void RequestSleep(const TimeSpan &interval)
Puts the current thread to sleep for a specified duration.
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
constexpr fltp08 elapsedSeconds() const
Returns the elapsed duration in seconds.
Definition TimeSpan.h:213
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:62
static Time SystemTime()
Retrieves the current system time which is a combination of std::chrono::steady_clock to ensure smoot...
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
The primary namespace for the NDEVR SDK.
@ ROLL
Rotation about the forward axis (X).
Definition Angle.h:45
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator The only requirement is that t_type have > ...
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
@ DEGREES
Angle measured in degrees (0 to 360 for a full circle).
Definition Angle.h:58
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
constexpr t_type clip(const t_type &value, const t_type &lower_bound, const t_type &upper_bound)
Clips the value given so that that the returned value falls between upper and lower bound.
A structure designed to store information about a specific Connection.
Definition Connection.h:90
static ApplicationOption< Angle< fltp08 > > realsense_roll
The roll angle offset for the RealSense sensor.