MBot Software Library  v1.0
An API documentation to mbot_firmware repository
mbot.h
1 #ifndef MBOT_H
2 #define MBOT_H
3 
4 #include <pico/stdlib.h>
5 #include <pico/mutex.h>
6 #include <pico/multicore.h>
7 #include <pico/time.h>
8 #include <hardware/gpio.h>
9 #include <mbot/motor/motor.h>
10 #include <mbot/encoder/encoder.h>
11 #include <mbot/motor/motor.h>
12 #include <mbot/defs/mbot_pins.h>
13 #include <mbot/defs/mbot_params.h>
14 #include <mbot/fram/fram.h>
15 #include <mbot/imu/imu.h>
16 #include <rc/math/filter.h>
17 #include <rc/mpu/mpu.h>
18 #include <comms/common.h>
19 #include <comms/protocol.h>
20 #include <comms/listener.h>
21 #include <comms/topic_data.h>
22 #include <comms/mbot_channels.h>
23 #include <mbot_lcm_msgs_serial.h>
24 
25 #include <math.h>
26 #include <inttypes.h>
27 
28 #define MESSAGE_CONFIRMATION_CHANNEL "MSG_CONFIRM"
29 
30 // TODO: Decide which controller is used, open loop = 1, PID = 0
31 #define OPEN_LOOP 1
32 
33 //Define drive type of this robot. See mbot_params.h.
34 // #define MBOT_DRIVE_TYPE OMNI_120_DRIVE
35 #define MBOT_DRIVE_TYPE DIFFERENTIAL_DRIVE
36 
37 extern mbot_bhy_data_t mbot_imu_data;
38 
39 // Global pointer to the i2c bus
40 static i2c_inst_t *i2c;
41 
42 // data to hold calibration coefficients
43 float coeffs[12]; // 4 calibration parameters per motor
44 
45 enum drive_modes{
46  MODE_MOTOR_PWM = 0,
47  MODE_MOTOR_VEL_OL = 1,
48  MODE_MOTOR_VEL_PID = 2,
49  MODE_MBOT_VEL = 3
50 };
51 
52 /*
53 * Messages used by the MBot code,
54 * we also use these to store state
55 */
56 // origin: mbot
57 serial_mbot_imu_t mbot_imu = {0};
58 serial_pose2D_t mbot_odometry = {0};
59 serial_mbot_encoders_t mbot_encoders = {0};
60 serial_twist2D_t mbot_vel = {0};
61 serial_mbot_motor_pwm_t mbot_motor_pwm = {0};
62 serial_mbot_motor_vel_t mbot_motor_vel = {0};
63 
64 // origin: comms
65 serial_twist2D_t mbot_vel_cmd = {0};
66 serial_mbot_motor_pwm_t mbot_motor_pwm_cmd = {0};
67 serial_mbot_motor_vel_t mbot_motor_vel_cmd = {0};
68 serial_timestamp_t mbot_received_time = {0};
69 
70 //callback functions
71 void timestamp_cb(serial_timestamp_t *msg);
72 void reset_encoders_cb(serial_mbot_encoders_t *msg);
73 void reset_odometry_cb(serial_pose2D_t *msg);
74 void mbot_vel_cmd_cb(serial_twist2D_t *msg);
75 void mbot_motor_vel_cmd_cb(serial_mbot_motor_vel_t *msg);
76 void mbot_motor_pwm_cmd_cb(serial_mbot_motor_pwm_t *msg);
77 bool mbot_loop(repeating_timer_t *rt);
78 void mbot_read_encoders(serial_mbot_encoders_t* encoders);
79 void mbot_calculate_motor_vel(serial_mbot_encoders_t encoders, serial_mbot_motor_vel_t *motor_vel);
80 
81 //helper functions
82 float _calibrated_pwm_from_vel_cmd(float vel_cmd, int motor_idx);
83 
84 #endif
Definition: imu.h:38