Skip to content

Commit b9c359f

Browse files
committed
camera_server: implement additional information fields
1 parent 6c17373 commit b9c359f

File tree

10 files changed

+545
-121
lines changed

10 files changed

+545
-121
lines changed

examples/camera_server/camera_server.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,22 @@ int main(int argc, char** argv)
8383

8484
// Finally call set_information() to "activate" the camera plugin.
8585

86-
camera_server->set_information({
86+
auto ret = camera_server->set_information({
8787
.vendor_name = "MAVSDK",
8888
.model_name = "Example Camera Server",
89+
.firmware_version = "1.0.0",
8990
.focal_length_mm = 3.0,
9091
.horizontal_sensor_size_mm = 3.68,
9192
.vertical_sensor_size_mm = 2.76,
9293
.horizontal_resolution_px = 3280,
9394
.vertical_resolution_px = 2464,
9495
});
9596

97+
if (ret != CameraServer::Result::Success) {
98+
std::cerr << "Failed to set camera info, exiting" << std::endl;
99+
exit(1);
100+
}
101+
96102
std::cout << "Connected to " << (system->is_standalone() ? "GCS" : "autopilot")
97103
<< " system ID " << +system->get_system_id() << std::endl;
98104
}

proto

src/mavsdk/plugins/camera_server/camera_server.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ CameraServer::Result CameraServer::respond_take_photo(CaptureInfo capture_info)
5050
bool operator==(const CameraServer::Information& lhs, const CameraServer::Information& rhs)
5151
{
5252
return (rhs.vendor_name == lhs.vendor_name) && (rhs.model_name == lhs.model_name) &&
53+
(rhs.firmware_version == lhs.firmware_version) &&
5354
((std::isnan(rhs.focal_length_mm) && std::isnan(lhs.focal_length_mm)) ||
5455
rhs.focal_length_mm == lhs.focal_length_mm) &&
5556
((std::isnan(rhs.horizontal_sensor_size_mm) &&
@@ -58,7 +59,10 @@ bool operator==(const CameraServer::Information& lhs, const CameraServer::Inform
5859
((std::isnan(rhs.vertical_sensor_size_mm) && std::isnan(lhs.vertical_sensor_size_mm)) ||
5960
rhs.vertical_sensor_size_mm == lhs.vertical_sensor_size_mm) &&
6061
(rhs.horizontal_resolution_px == lhs.horizontal_resolution_px) &&
61-
(rhs.vertical_resolution_px == lhs.vertical_resolution_px);
62+
(rhs.vertical_resolution_px == lhs.vertical_resolution_px) &&
63+
(rhs.lens_id == lhs.lens_id) &&
64+
(rhs.definition_file_version == lhs.definition_file_version) &&
65+
(rhs.definition_file_uri == lhs.definition_file_uri);
6266
}
6367

6468
std::ostream& operator<<(std::ostream& str, CameraServer::Information const& information)
@@ -67,11 +71,15 @@ std::ostream& operator<<(std::ostream& str, CameraServer::Information const& inf
6771
str << "information:" << '\n' << "{\n";
6872
str << " vendor_name: " << information.vendor_name << '\n';
6973
str << " model_name: " << information.model_name << '\n';
74+
str << " firmware_version: " << information.firmware_version << '\n';
7075
str << " focal_length_mm: " << information.focal_length_mm << '\n';
7176
str << " horizontal_sensor_size_mm: " << information.horizontal_sensor_size_mm << '\n';
7277
str << " vertical_sensor_size_mm: " << information.vertical_sensor_size_mm << '\n';
7378
str << " horizontal_resolution_px: " << information.horizontal_resolution_px << '\n';
7479
str << " vertical_resolution_px: " << information.vertical_resolution_px << '\n';
80+
str << " lens_id: " << information.lens_id << '\n';
81+
str << " definition_file_version: " << information.definition_file_version << '\n';
82+
str << " definition_file_uri: " << information.definition_file_uri << '\n';
7583
str << '}';
7684
return str;
7785
}

src/mavsdk/plugins/camera_server/camera_server_impl.cpp

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,66 @@ void CameraServerImpl::disable()
145145
stop_image_capture_interval();
146146
}
147147

148+
/**
149+
* Parses a firmware version string.
150+
*
151+
* The string must be in the format "<major>.<minor>.<patch>.<dev>".
152+
*
153+
* @param [in] version_str The version string to be parsed.
154+
* @return True if parsing was successful, otherwise false.
155+
*/
156+
bool CameraServerImpl::parse_version_string(std::string& version_str)
157+
{
158+
uint32_t unused;
159+
160+
return parse_version_string(version_str, unused);
161+
}
162+
163+
/**
164+
* Parses a firmware version string.
165+
*
166+
* The string must be in the format "<major>[.<minor>[.<patch>[.<dev>]]]".
167+
*
168+
* @param [in] version_str The version string to be parsed.
169+
* @param [out] version Encoded version integer for passing to MAVSDK messages.
170+
* @return True if parsing was successful, otherwise false.
171+
*/
172+
bool CameraServerImpl::parse_version_string(std::string& version_str, uint32_t& version)
173+
{
174+
// empty string means no version
175+
if (version_str.empty()) {
176+
version = 0;
177+
178+
return true;
179+
}
180+
181+
uint8_t major{}, minor{}, patch{}, dev{};
182+
183+
auto ret = sscanf(version_str.c_str(), "%hhu.%hhu.%hhu.%hhu", &major, &minor, &patch, &dev);
184+
185+
if (ret == EOF) {
186+
return false;
187+
}
188+
189+
// pack version according to MAVLINK spec
190+
version = dev << 24 | patch << 16 | minor << 8 | major;
191+
192+
return true;
193+
}
194+
148195
CameraServer::Result CameraServerImpl::set_information(CameraServer::Information information)
149196
{
197+
if (!parse_version_string(information.firmware_version)) {
198+
LogDebug() << "incorrectly formatted firmware version string: "
199+
<< information.firmware_version;
200+
return CameraServer::Result::WrongArgument;
201+
}
202+
203+
// TODO: validate information.definition_file_uri
204+
150205
_is_information_set = true;
151206
_information = information;
207+
152208
return CameraServer::Result::Success;
153209
}
154210

@@ -285,11 +341,10 @@ std::optional<mavlink_message_t> CameraServerImpl::process_camera_information_re
285341
// FIXME: why is this needed to prevent dropping messages?
286342
std::this_thread::sleep_for(std::chrono::milliseconds(100));
287343

288-
// unsupported items
289-
const uint32_t firmware_version = 0;
290-
const uint8_t lens_id = 0;
291-
const uint16_t camera_definition_version = 0;
292-
auto camera_definition_uri = "";
344+
// It is safe to ignore the return value of parse_version_string() here
345+
// since the string was already validated in set_information().
346+
uint32_t firmware_version;
347+
parse_version_string(_information.firmware_version, firmware_version);
293348

294349
// capability flags are determined by subscriptions
295350
uint32_t capability_flags{};
@@ -312,10 +367,10 @@ std::optional<mavlink_message_t> CameraServerImpl::process_camera_information_re
312367
_information.vertical_sensor_size_mm,
313368
_information.horizontal_resolution_px,
314369
_information.vertical_resolution_px,
315-
lens_id,
370+
_information.lens_id,
316371
capability_flags,
317-
camera_definition_version,
318-
camera_definition_uri);
372+
_information.definition_file_version,
373+
_information.definition_file_uri.c_str());
319374

320375
_parent->send_message(msg);
321376
LogDebug() << "sent info msg";

src/mavsdk/plugins/camera_server/camera_server_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class CameraServerImpl : public PluginImplBase {
4949

5050
CameraServer::TakePhotoCallback _take_photo_callback{};
5151

52+
bool parse_version_string(std::string& version_str);
53+
bool parse_version_string(std::string& version_str, uint32_t& version);
5254
void start_image_capture_interval(float interval, int32_t count, int32_t index);
5355
void stop_image_capture_interval();
5456

src/mavsdk/plugins/camera_server/include/plugins/camera_server/camera_server.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,18 @@ class CameraServer : public PluginBase {
6262
struct Information {
6363
std::string vendor_name{}; /**< @brief Name of the camera vendor */
6464
std::string model_name{}; /**< @brief Name of the camera model */
65+
std::string firmware_version{}; /**< @brief Camera firmware version in
66+
'<major>.<minor>.<patch>.<build>' format */
6567
float focal_length_mm{}; /**< @brief Focal length */
6668
float horizontal_sensor_size_mm{}; /**< @brief Horizontal sensor size */
6769
float vertical_sensor_size_mm{}; /**< @brief Vertical sensor size */
6870
uint32_t horizontal_resolution_px{}; /**< @brief Horizontal image resolution in pixels */
6971
uint32_t vertical_resolution_px{}; /**< @brief Vertical image resolution in pixels */
72+
uint32_t lens_id{}; /**< @brief Lens ID */
73+
uint32_t
74+
definition_file_version{}; /**< @brief Camera definition file version (iteration) */
75+
std::string
76+
definition_file_uri{}; /**< @brief Camera definition URI (http or mavlink ftp) */
7077
};
7178

7279
/**
@@ -198,7 +205,8 @@ class CameraServer : public PluginBase {
198205
using ResultCallback = std::function<void(Result)>;
199206

200207
/**
201-
* @brief Sets the camera information
208+
* @brief Sets the camera information. This must be called as soon as the camera server is
209+
* created.
202210
*
203211
* This function is blocking.
204212
*
@@ -207,7 +215,8 @@ class CameraServer : public PluginBase {
207215
Result set_information(Information information) const;
208216

209217
/**
210-
* @brief Sets the camera capture status
218+
* @brief Sets image capture in progress status flags. This should be set to true when the
219+
* camera is busy taking a photo and false when it is done.
211220
*
212221
* This function is blocking.
213222
*
@@ -222,12 +231,13 @@ class CameraServer : public PluginBase {
222231
using TakePhotoCallback = std::function<void(Result, int32_t)>;
223232

224233
/**
225-
* @brief Subscribe to single-image capture commands
234+
* @brief Subscribe to image capture requests. Each request received should respond to using
235+
* RespondTakePhoto.
226236
*/
227237
void subscribe_take_photo(TakePhotoCallback callback);
228238

229239
/**
230-
* @brief Respond to a single-image capture command.
240+
* @brief Respond to an image capture request from SubscribeTakePhoto.
231241
*
232242
* This function is blocking.
233243
*

src/mavsdk_server/src/generated/camera_server/camera_server.grpc.pb.h

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)