|
|
@@ -1,11 +1,11 @@
|
|
|
# RT-Robot 使用示例
|
|
|
|
|
|
-使用 rt-robot 框架的初始化流程包括:
|
|
|
+使用 rt-robot 框架的初始化智能车流程包括:
|
|
|
|
|
|
-- 1 初始化车轮 (为车轮添加电机、编码器、PID)
|
|
|
- - 1.1 初始化电机
|
|
|
- - 1.2 初始化电编码器
|
|
|
- - 1.3 初始化 PID
|
|
|
+- 1 初始化车轮 (为车轮添加电机、编码器、控制器)
|
|
|
+ - 1.1 初始化车轮电机
|
|
|
+ - 1.2 初始化车轮编码器
|
|
|
+ - 1.3 初始化车轮控制器
|
|
|
|
|
|
- 2 初始化动力学模型
|
|
|
|
|
|
@@ -14,79 +14,116 @@
|
|
|
下面是两轮差分控制的小车初始化示例代码:
|
|
|
|
|
|
```C
|
|
|
-// 1. Initialize two wheels - left and right
|
|
|
-wheel_t* c_wheels = (wheel_t*) rt_malloc(sizeof(wheel_t) * 2);
|
|
|
-if (c_wheels == RT_NULL)
|
|
|
+// MOTOR
|
|
|
+#define LEFT_FORWARD_PWM "pwm4"
|
|
|
+#define LEFT_FORWARD_PWM_CHANNEL 3
|
|
|
+#define LEFT_BACKWARD_PWM "pwm4"
|
|
|
+#define LEFT_BACKWARD_PWM_CHANNEL 4
|
|
|
+
|
|
|
+#define RIGHT_FORWARD_PWM "pwm2"
|
|
|
+#define RIGHT_FORWARD_PWM_CHANNEL 3
|
|
|
+#define RIGHT_BACKWARD_PWM "pwm2"
|
|
|
+#define RIGHT_BACKWARD_PWM_CHANNEL 4
|
|
|
+
|
|
|
+// ENCODER
|
|
|
+#define LEFT_ENCODER_A_PHASE_PIN 31 // GET_PIN(B, 15)
|
|
|
+#define LEFT_ENCODER_B_PHASE_PIN 34 // GET_PIN(C, 2)
|
|
|
+#define RIGHT_ENCODER_A_PHASE_PIN 36 //
|
|
|
+#define RIGHT_ENCODER_B_PHASE_PIN 8 //
|
|
|
+#define PULSE_PER_REVOL 2000 // Real value 2000
|
|
|
+#define ENCODER_SAMPLE_TIME 50
|
|
|
+
|
|
|
+// CONTROLLER PID
|
|
|
+#define PID_SAMPLE_TIME 50
|
|
|
+#define PID_PARAM_KP 6.6
|
|
|
+#define PID_PARAM_KI 6.5
|
|
|
+#define PID_PARAM_KD 7.6
|
|
|
+
|
|
|
+// WHEEL
|
|
|
+#define WHEEL_RADIUS 0.066
|
|
|
+#define GEAR_RATIO 36
|
|
|
+
|
|
|
+// CAR
|
|
|
+chassis_t chas;
|
|
|
+
|
|
|
+#define WHEEL_DIST_X 0
|
|
|
+#define WHEEL_DIST_Y 0.13
|
|
|
+
|
|
|
+// Car Thread
|
|
|
+#define THREAD_PRIORITY 10
|
|
|
+#define THREAD_STACK_SIZE 512
|
|
|
+#define THREAD_TIMESLICE 5
|
|
|
+
|
|
|
+static rt_thread_t tid_car = RT_NULL;
|
|
|
+
|
|
|
+static void command_register_all(void);
|
|
|
+void init_laser_and_gimbal(void);
|
|
|
+
|
|
|
+void car_thread(void *param)
|
|
|
{
|
|
|
- LOG_D("Failed to malloc memory for wheels");
|
|
|
-}
|
|
|
-
|
|
|
-// 1.1 Create two motors
|
|
|
-motor_t left_motor = motor_create(left_motor_init, left_motor_enable, left_motor_disable, left_motor_set_speed, DC_MOTOR);
|
|
|
-motor_t right_motor = motor_create(right_motor_init, right_motor_enable, right_motor_disable, right_motor_set_speed, DC_MOTOR);
|
|
|
-
|
|
|
-// 1.2 Create two encoders
|
|
|
-encoder_t left_encoder = encoder_create(LEFT_ENCODER_PIN, PULSE_PER_REVOL);
|
|
|
-encoder_t right_encoder = encoder_create(RIGHT_ENCODER_PIN, PULSE_PER_REVOL);
|
|
|
-
|
|
|
-// 1.3 Create two pid contollers
|
|
|
-pid_control_t left_pid = pid_create();
|
|
|
-pid_control_t right_pid = pid_create();
|
|
|
-
|
|
|
-// 1.4 Add two wheels
|
|
|
-c_wheels[0] = wheel_create(left_motor, left_encoder, left_pid, WHEEL_RADIUS, GEAR_RATIO);
|
|
|
-c_wheels[1] = wheel_create(right_motor, right_encoder, right_pid, WHEEL_RADIUS, GEAR_RATIO);
|
|
|
-
|
|
|
-// 2. Iinialize Kinematics - Two Wheel Differential Drive
|
|
|
-kinematics_t c_kinematics = kinematics_create(TWO_WD, WHEEL_DIST_X, WHEEL_DIST_Y, WHEEL_RADIUS);
|
|
|
-
|
|
|
-// 3. Initialize Chassis
|
|
|
-chas = chassis_create(c_wheels, c_kinematics);
|
|
|
-
|
|
|
-// 4. Set Sample time
|
|
|
-encoder_set_sample_time(chas->c_wheels[0]->w_encoder, SAMPLE_TIME);
|
|
|
-encoder_set_sample_time(chas->c_wheels[1]->w_encoder, SAMPLE_TIME);
|
|
|
-pid_set_sample_time(chas->c_wheels[0]->w_pid, PID_SAMPLE_TIME);
|
|
|
-pid_set_sample_time(chas->c_wheels[1]->w_pid, PID_SAMPLE_TIME);
|
|
|
+ // TODO
|
|
|
|
|
|
-// 5. Enable Chassis
|
|
|
-chassis_enable(chas);
|
|
|
+ struct velocity target_velocity;
|
|
|
|
|
|
-// Turn left
|
|
|
-target_vel.linear_x = 0; // m/s
|
|
|
-target_vel.linear_y = 0; // m/s
|
|
|
-target_vel.angular_z = PI / 4; // rad/s
|
|
|
+ target_velocity.linear_x = 0.00f;
|
|
|
+ target_velocity.linear_y = 0;
|
|
|
+ target_velocity.angular_z = 0;
|
|
|
+ chassis_set_velocity(chas, target_velocity);
|
|
|
|
|
|
-chassis_set_velocity(chas, target_vel);
|
|
|
-rt_thread_mdelay(500);
|
|
|
+ // Open loop control
|
|
|
+ // controller_disable(chas->c_wheels[0]->w_controller);
|
|
|
+ // controller_disable(chas->c_wheels[1]->w_controller);
|
|
|
|
|
|
-// Turn right
|
|
|
-target_vel.linear_x = 0; // m/s
|
|
|
-target_vel.linear_y = 0; // m/s
|
|
|
-target_vel.angular_z = - PI / 4; // rad/s
|
|
|
-
|
|
|
-chassis_set_velocity(chas, target_vel);
|
|
|
-rt_thread_mdelay(500);
|
|
|
-
|
|
|
-// Go straight
|
|
|
-target_vel.linear_x = 0.06; // m/s
|
|
|
-target_vel.linear_y = 0; // m/s
|
|
|
-target_vel.angular_z = 0;
|
|
|
-
|
|
|
-chassis_set_velocity(chas, target_vel);
|
|
|
-
|
|
|
-// Stop
|
|
|
-target_vel.linear_x = 0.0; // m/s
|
|
|
-target_vel.linear_y = 0; // m/s
|
|
|
-target_vel.angular_z = 0; // rad/s
|
|
|
-
|
|
|
-chassis_set_velocity(chas, target_vel);
|
|
|
-rt_thread_mdelay(500);
|
|
|
+ while (1)
|
|
|
+ {
|
|
|
+ rt_thread_mdelay(50);
|
|
|
+ chassis_update(chas);
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
-while(1)
|
|
|
+void car_init(void *parameter)
|
|
|
{
|
|
|
- chassis_update(chas);
|
|
|
- rt_thread_mdelay(50);
|
|
|
+ // 1. Initialize two wheels - left and right
|
|
|
+ wheel_t* c_wheels = (wheel_t*) rt_malloc(sizeof(wheel_t) * 2);
|
|
|
+ if (c_wheels == RT_NULL)
|
|
|
+ {
|
|
|
+ LOG_D("Failed to malloc memory for wheels");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1.1 Create two motors
|
|
|
+ dual_pwm_motor_t left_motor = dual_pwm_motor_create(LEFT_FORWARD_PWM, LEFT_FORWARD_PWM_CHANNEL, LEFT_BACKWARD_PWM, LEFT_BACKWARD_PWM_CHANNEL);
|
|
|
+ dual_pwm_motor_t right_motor = dual_pwm_motor_create(RIGHT_FORWARD_PWM, RIGHT_FORWARD_PWM_CHANNEL, RIGHT_BACKWARD_PWM, RIGHT_BACKWARD_PWM_CHANNEL);
|
|
|
+
|
|
|
+ // 1.2 Create two encoders
|
|
|
+ ab_phase_encoder_t left_encoder = ab_phase_encoder_create(LEFT_ENCODER_A_PHASE_PIN, LEFT_ENCODER_B_PHASE_PIN, PULSE_PER_REVOL, ENCODER_SAMPLE_TIME);
|
|
|
+ ab_phase_encoder_t right_encoder = ab_phase_encoder_create(RIGHT_ENCODER_A_PHASE_PIN, RIGHT_ENCODER_B_PHASE_PIN, PULSE_PER_REVOL, ENCODER_SAMPLE_TIME);
|
|
|
+
|
|
|
+ // 1.3 Create two pid contollers
|
|
|
+ inc_pid_controller_t left_pid = inc_pid_controller_create(PID_PARAM_KP, PID_PARAM_KI, PID_PARAM_KD, PID_SAMPLE_TIME);
|
|
|
+ inc_pid_controller_t right_pid = inc_pid_controller_create(PID_PARAM_KP, PID_PARAM_KI, PID_PARAM_KD, PID_SAMPLE_TIME);
|
|
|
+
|
|
|
+ // 1.4 Add two wheels
|
|
|
+ c_wheels[0] = wheel_create((motor_t)left_motor, (encoder_t)left_encoder, (controller_t)left_pid, WHEEL_RADIUS, GEAR_RATIO);
|
|
|
+ c_wheels[1] = wheel_create((motor_t)right_motor, (encoder_t)right_encoder, (controller_t)right_pid, WHEEL_RADIUS, GEAR_RATIO);
|
|
|
+
|
|
|
+ // 2. Iinialize Kinematics - Two Wheel Differential Drive
|
|
|
+ kinematics_t c_kinematics = kinematics_create(TWO_WD, WHEEL_DIST_X, WHEEL_DIST_Y, WHEEL_RADIUS);
|
|
|
+
|
|
|
+ // 3. Initialize Chassis
|
|
|
+ chas = chassis_create(c_wheels, c_kinematics);
|
|
|
+
|
|
|
+ // 4. Enable Chassis
|
|
|
+ chassis_enable(chas);
|
|
|
+
|
|
|
+ // thread
|
|
|
+ tid_car = rt_thread_create("tcar",
|
|
|
+ car_thread, RT_NULL,
|
|
|
+ THREAD_STACK_SIZE,
|
|
|
+ THREAD_PRIORITY, THREAD_TIMESLICE);
|
|
|
+
|
|
|
+ if (tid_car != RT_NULL)
|
|
|
+ {
|
|
|
+ rt_thread_startup(tid_car);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
```
|