2 * This file is part of INAV.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with INAV. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef USE_SERIAL_GIMBAL
26 #include <build/debug.h>
27 #include <config/parameter_group_ids.h>
29 #include "common/time.h"
33 #include "drivers/gimbal_common.h"
36 #include "settings_generated.h"
39 PG_REGISTER_WITH_RESET_TEMPLATE(gimbalConfig_t
, gimbalConfig
, PG_GIMBAL_CONFIG
, 1);
41 PG_RESET_TEMPLATE(gimbalConfig_t
, gimbalConfig
,
42 .panChannel
= SETTING_GIMBAL_PAN_CHANNEL_DEFAULT
,
43 .tiltChannel
= SETTING_GIMBAL_TILT_CHANNEL_DEFAULT
,
44 .rollChannel
= SETTING_GIMBAL_ROLL_CHANNEL_DEFAULT
,
45 .sensitivity
= SETTING_GIMBAL_SENSITIVITY_DEFAULT
,
46 .panTrim
= SETTING_GIMBAL_PAN_TRIM_DEFAULT
,
47 .tiltTrim
= SETTING_GIMBAL_TILT_TRIM_DEFAULT
,
48 .rollTrim
= SETTING_GIMBAL_ROLL_TRIM_DEFAULT
51 static gimbalDevice_t
*commonGimbalDevice
= NULL
;
53 void gimbalCommonInit(void)
57 void gimbalCommonSetDevice(gimbalDevice_t
*gimbalDevice
)
59 SD(fprintf(stderr
, "[GIMBAL]: device added %p\n", gimbalDevice
));
60 commonGimbalDevice
= gimbalDevice
;
63 gimbalDevice_t
*gimbalCommonDevice(void)
65 return commonGimbalDevice
;
68 void gimbalCommonProcess(gimbalDevice_t
*gimbalDevice
, timeUs_t currentTimeUs
)
70 if (gimbalDevice
&& gimbalDevice
->vTable
->process
&& gimbalCommonIsReady(gimbalDevice
)) {
71 gimbalDevice
->vTable
->process(gimbalDevice
, currentTimeUs
);
75 gimbalDevType_e
gimbalCommonGetDeviceType(gimbalDevice_t
*gimbalDevice
)
77 if (!gimbalDevice
|| !gimbalDevice
->vTable
->getDeviceType
) {
78 return GIMBAL_DEV_UNKNOWN
;
81 return gimbalDevice
->vTable
->getDeviceType(gimbalDevice
);
84 bool gimbalCommonIsReady(gimbalDevice_t
*gimbalDevice
)
86 if (gimbalDevice
&& gimbalDevice
->vTable
->isReady
) {
87 return gimbalDevice
->vTable
->isReady(gimbalDevice
);
92 #ifdef GIMBAL_UNIT_TEST
93 void taskUpdateGimbal(timeUs_t currentTimeUs
)
97 void taskUpdateGimbal(timeUs_t currentTimeUs
)
103 gimbalDevice_t
*gimbalDevice
= gimbalCommonDevice();
106 gimbalCommonProcess(gimbalDevice
, currentTimeUs
);
111 // TODO: check if any gimbal types are enabled
112 bool gimbalCommonIsEnabled(void)
118 bool gimbalCommonHtrkIsEnabled(void)
120 const gimbalDevice_t
*dev
= gimbalCommonDevice();
121 if(dev
&& dev
->vTable
->hasHeadTracker
) {
122 bool ret
= dev
->vTable
->hasHeadTracker(dev
);
130 int16_t gimbalCommonGetPanPwm(const gimbalDevice_t
*gimbalDevice
)
132 if (gimbalDevice
&& gimbalDevice
->vTable
->getGimbalPanPWM
) {
133 return gimbalDevice
->vTable
->getGimbalPanPWM(gimbalDevice
);
136 return gimbalDevice
? gimbalDevice
->currentPanPWM
: PWM_RANGE_MIDDLE
+ gimbalConfig()->panTrim
;