Skip to content

Commit 38d1797

Browse files
committed
SITL: add camera simulation object
Add a new SIM_Camera object to SITL, similar to existing Sprayer and Gripper payload simulations. The camera simulation can be enabled via SIM_CAM_ENABLE and supports triggering image capture via: - servo output (TRIG_SRV + TRIG_PWM) - GPIO pin (GPIO) When triggered, the camera increments an internal image counter and sends a GCS message indicating capture.
1 parent c21818d commit 38d1797

6 files changed

Lines changed: 150 additions & 0 deletions

File tree

libraries/SITL/SIM_Aircraft.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,11 @@ void Aircraft::update_external_payload(const struct sitl_input &input)
11061106
{
11071107
external_payload_mass = 0;
11081108

1109+
// update camera
1110+
if (camera && camera->is_enabled()) {
1111+
camera->update(input);
1112+
}
1113+
11091114
// update sprayer
11101115
if (sprayer && sprayer->is_enabled()) {
11111116
sprayer->update(input);

libraries/SITL/SIM_Aircraft.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "SITL.h"
2626
#include "SITL_Input.h"
27+
#include "SIM_Camera.h"
2728
#include "SIM_Sprayer.h"
2829
#include "SIM_Gripper_Servo.h"
2930
#include "SIM_Gripper_EPM.h"
@@ -154,6 +155,7 @@ class Aircraft {
154155
float get_home_yaw() const { return home_yaw; }
155156

156157
void set_buzzer(Buzzer *_buzzer) { buzzer = _buzzer; }
158+
void set_camera(Camera *_camera) { camera = _camera; }
157159
void set_sprayer(Sprayer *_sprayer) { sprayer = _sprayer; }
158160
void set_parachute(Parachute *_parachute) { parachute = _parachute; }
159161
void set_richenpower(RichenPower *_richenpower) { richenpower = _richenpower; }
@@ -389,6 +391,7 @@ class Aircraft {
389391
} smoothing;
390392

391393
Buzzer *buzzer;
394+
Camera *camera;
392395
Sprayer *sprayer;
393396
Gripper_Servo *gripper;
394397
Gripper_EPM *gripper_epm;

libraries/SITL/SIM_Camera.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
This program is free software: you can redistribute it and/or modify
3+
it under the terms of the GNU General Public License as published by
4+
the Free Software Foundation, either version 3 of the License, or
5+
(at your option) any later version.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
/*
16+
simple camera simulator class
17+
*/
18+
19+
#include "SIM_Camera.h"
20+
21+
#include <GCS_MAVLink/GCS.h>
22+
#include <SITL/SITL.h>
23+
24+
using namespace SITL;
25+
26+
// table of user settable parameters
27+
const AP_Param::GroupInfo Camera::var_info[] = {
28+
29+
// @Param: ENABLE
30+
// @DisplayName: Camera Sim enable/disable
31+
// @Description: Allows you to enable (1) or disable (0) the Camera simulation
32+
// @Values: 0:Disabled,1:Enabled
33+
// @User: Advanced
34+
AP_GROUPINFO("ENABLE", 0, Camera, _enable, 0),
35+
36+
// @Param: TRIG_SRV
37+
// @DisplayName: Camera trigger servo pin
38+
// @Description: The pin number that the Camera trigger servo is connected to. (start at 1)
39+
// @Range: 0 32
40+
// @User: Advanced
41+
AP_GROUPINFO("TRIG_SRV", 1, Camera, _trigger_servo_pin, -1),
42+
43+
// @Param: TRIG_GPIO
44+
// @DisplayName: Camera trigger GPIO pin
45+
// @Description: The GPIO pin number used to trigger Camera capture.
46+
// @Range: -1 15
47+
// @User: Advanced
48+
AP_GROUPINFO("GPIO_PIN", 2, Camera, _trigger_gpio_pin, -1),
49+
50+
// @Param: TRIG_PWM
51+
// @DisplayName: Camera trigger PWM threshold
52+
// @Description: PWM threshold for considering the camera trigger servo active.
53+
// @Range: 1000 2000
54+
// @Units: PWM
55+
// @User: Advanced
56+
AP_GROUPINFO("TRIG_PWM", 3, Camera, _trigger_pwm, 1300),
57+
58+
AP_GROUPEND
59+
};
60+
61+
/*
62+
update camera state
63+
*/
64+
void Camera::update(const struct sitl_input &input)
65+
{
66+
bool servo_triggered = false;
67+
if (_trigger_servo_pin >= 1 && _trigger_servo_pin <= 32) {
68+
const int16_t servo_pwm = input.servos[_trigger_servo_pin - 1];
69+
servo_triggered = servo_pwm >= _trigger_pwm;
70+
}
71+
72+
bool gpio_triggered = false;
73+
const auto *sitl = AP::sitl();
74+
if (sitl != nullptr && _trigger_gpio_pin >= 0 && _trigger_gpio_pin <= 15) {
75+
gpio_triggered = (sitl->pin_mask.get() & (1U << _trigger_gpio_pin)) != 0;
76+
}
77+
78+
const bool trigger_active = servo_triggered || gpio_triggered;
79+
if (trigger_active && !_last_trigger_state) {
80+
_image_count++;
81+
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "SITL: Camera image %u", (unsigned)_image_count);
82+
}
83+
84+
_last_trigger_state = trigger_active;
85+
}

libraries/SITL/SIM_Camera.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
This program is free software: you can redistribute it and/or modify
3+
it under the terms of the GNU General Public License as published by
4+
the Free Software Foundation, either version 3 of the License, or
5+
(at your option) any later version.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
/*
16+
simple camera simulation class
17+
*/
18+
19+
#pragma once
20+
21+
#include "stdint.h"
22+
#include <AP_Param/AP_Param.h>
23+
#include "SITL_Input.h"
24+
25+
namespace SITL {
26+
27+
class Camera {
28+
public:
29+
Camera() {
30+
AP_Param::setup_object_defaults(this, var_info);
31+
}
32+
33+
// update camera state
34+
void update(const struct sitl_input &input);
35+
36+
static const struct AP_Param::GroupInfo var_info[];
37+
38+
bool is_enabled() const { return static_cast<bool>(_enable); }
39+
uint32_t image_count() const { return _image_count; }
40+
41+
private:
42+
AP_Int8 _enable;
43+
AP_Int8 _trigger_servo_pin;
44+
AP_Int8 _trigger_gpio_pin;
45+
AP_Int16 _trigger_pwm;
46+
47+
bool _last_trigger_state;
48+
uint32_t _image_count;
49+
};
50+
51+
}

libraries/SITL/SITL.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ const AP_Param::GroupInfo SIM::var_info3[] = {
655655
AP_SUBGROUPINFO(vicon, "VICON_", 56, SIM, ViconParms),
656656
#endif // AP_SIM_VICON_ENABLED
657657

658+
// @Group: CAM_
659+
// @Path: ./SIM_Camera.cpp
660+
AP_SUBGROUPINFO(camera_sim, "CAM_", 57, SIM, Camera),
661+
658662
#ifdef SFML_JOYSTICK
659663
AP_SUBGROUPEXTENSION("", 63, SIM, var_sfml_joystick),
660664
#endif // SFML_JOYSTICK

libraries/SITL/SITL.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <AP_InertialSensor/AP_InertialSensor.h>
1414

1515
#include "SIM_Buzzer.h"
16+
#include "SIM_Camera.h"
1617
#include "SIM_Gripper_EPM.h"
1718
#include "SIM_Gripper_Servo.h"
1819
#include "SIM_I2C.h"
@@ -542,6 +543,7 @@ class SIM {
542543
}
543544

544545
Sprayer sprayer_sim;
546+
Camera camera_sim;
545547

546548
Gripper_Servo gripper_sim;
547549
Gripper_EPM gripper_epm_sim;

0 commit comments

Comments
 (0)