Skip to content

Commit

Permalink
Main gimbal control and limits
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasP850 committed Jan 4, 2025
1 parent 065f657 commit a763acc
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 25 deletions.
25 changes: 21 additions & 4 deletions app/inc/gimbal_task.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
#ifndef GIMBAL_TASK_H
#define GIMBAL_TASK_H

#define YAW_MID_POSITION
#define PITCH_MID_POSITION
// TODO: Find correct values

#define PITCH_VELOCITY_SCALE (0.01f)
#define YAW_VELOCITY_SCALE (0.01f)
// Note: these values are in degrees so that they are human-readable

#define PITCH_LOWER_LIMIT (0.0f)
#define PITCH_UPPER_LIMIT (45.0f)

#define PITCH_OFFSET (0.0f)
#define YAW_OFFSET (0.0f)

#define PITCH_CONTROLLER_VELOCITY_COEF (1e-6f)
#define YAW_CONTORLLER_VELOCITY_COEF (5e-5f)

#define PITCH_MOUSE_VELOCITY_COEF (5e-5f)
#define YAW_MOUSE_VELOCITY_COEF (1e-5f)

// TODO: Find correct values

// Degrees per second

#define PITCH_RATE_LIMIT (1.0f)
#define YAW_RATE_LIMIT (1.0f)

typedef struct
{
Expand Down
80 changes: 60 additions & 20 deletions app/src/gimbal_task.c
Original file line number Diff line number Diff line change
@@ -1,39 +1,79 @@
#include "gimbal_task.h"

#include "dji_motor.h"
#include "imu_task.h"
#include "jetson_orin.h"
#include "pid.h"
#include "rate_limiter.h"
#include "robot.h"
#include "remote.h"
#include "user_math.h"

extern Robot_State_t g_robot_state;
extern Remote_t g_remote;
extern IMU_t g_imu;
extern Jetson_Orin_Data_t g_orin_data;

DJI_Motor_Handle_t *g_gimbal_motors[2];
DJI_Motor_Handle_t* g_pitch_motor;
DJI_Motor_Handle_t* g_yaw_motor;
rate_limiter_t g_yaw_rate_limiter;
rate_limiter_t g_pitch_rate_limiter;

void Gimbal_Task_Init()
{
for (int i = 0; i < 2; i++)
{
Motor_Config_t chassis_motor_config = {
// TODO: Work with gear ratios
Motor_Config_t pitch_motor_config = {
.can_bus = 2,
.speed_controller_id = i+1,
.offset = 0,
.control_mode = POSITION,
.speed_controller_id = 0,
.offset = PITCH_OFFSET,
.control_mode = POSITION_CONTROL,
.motor_reversal = MOTOR_REVERSAL_NORMAL,
.angle =
{
.kp = 1000.0f,
.ki = 0.0f,
.kd = 100.0f,
.output_limit = M2006_MAX_CURRENT,
}};
g_chassis_motors[i] = DJI_Motor_Init(&chassis_motor_config, M2006);
}
.angle_pid = {
.kp = 1000.0f,
.ki = 0.0f,
.kd = 100.0f,
.output_limit = M2006_MAX_CURRENT,
}
};
g_pitch_motor = DJI_Motor_Init(&pitch_motor_config, M2006);

Motor_Config_t yaw_motor_config = {
.can_bus = 2,
.speed_controller_id = 1,
.offset = YAW_OFFSET,
.control_mode = POSITION_CONTROL,
.motor_reversal = MOTOR_REVERSAL_NORMAL,
.angle_pid = {
.kp = 1000.0f,
.ki = 0.0f,
.kd = 100.0f,
.output_limit = M2006_MAX_CURRENT,
}
};
g_pitch_motor = DJI_Motor_Init(&yaw_motor_config, M2006);

rate_limiter_init(&g_yaw_rate_limiter, YAW_RATE_LIMIT);
rate_limiter_init(&g_pitch_rate_limiter, PITCH_RATE_LIMIT);

}

void Gimbal_Ctrl_Loop()
{
float yaw_velocity = g_remote.controller.right_stick.x;
float pitch_velocity = g_remote.controller.right_stick.y;
g_robot_state.gimbal.pitch_angle += pitch_velocity * 0.01f;
g_robot_state.gimbal.yaw_angle += yaw_velocity * 0.01f;

if (g_robot_state.launch.IS_AUTO_AIMING_ENABLED) {
if (g_orin_data.receiving.auto_aiming.pitch || g_orin_data.receiving.auto_aiming.yaw) {
// We can set these directly for now, they will be limited later in this function
g_robot_state.gimbal.yaw_angle = (g_imu.deg.yaw - g_orin_data.receiving.auto_aiming.yaw) * DEG_TO_RAD;
g_robot_state.gimbal.pitch_angle = (g_imu.deg.pitch - g_orin_data.receiving.auto_aiming.pitch) * DEG_TO_RAD;
}
}

g_robot_state.gimbal.yaw_angle = fmod(g_robot_state.gimbal.yaw_angle, 2 * PI);
__MAX_LIMIT(g_robot_state.gimbal.pitch_angle, PITCH_LOWER_LIMIT, PITCH_UPPER_LIMIT);

g_robot_state.gimbal.yaw_angle = rate_limiter(&g_yaw_rate_limiter, g_robot_state.gimbal.yaw_angle);
g_robot_state.gimbal.pitch_angle = rate_limiter(&g_pitch_rate_limiter, g_robot_state.gimbal.pitch_angle);

DJI_Motor_Set_Angle(g_yaw_motor, g_robot_state.gimbal.yaw_angle);
DJI_Motor_Set_Angle(g_pitch_motor, g_robot_state.gimbal.pitch_angle);
}
10 changes: 9 additions & 1 deletion app/src/robot.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ void Handle_Disabled_State()

void Process_Remote_Input()
{
// USER CODE HERE
// Gimbal control
g_robot_state.gimbal.yaw_angle -= (
g_remote.controller.right_stick.x * YAW_CONTORLLER_VELOCITY_COEF +
g_remote.mouse.x * YAW_MOUSE_VELOCITY_COEF
);
g_robot_state.gimbal.pitch_angle -= (
g_remote.controller.right_stick.y * PITCH_CONTROLLER_VELOCITY_COEF -
g_remote.mouse.y * PITCH_MOUSE_VELOCITY_COEF
);
}

void Process_Chassis_Control()
Expand Down

0 comments on commit a763acc

Please sign in to comment.