Mecanum robot firmware for WRS2020 Ususama
Dependencies: UsusamaSerial MecanumInterface PMSU_100 PIDController IMUInterface WaypointManager i2cmaster MecanumController
main.cpp@1:2720d0e8b2f1, 2021-08-24 (annotated)
- Committer:
- sgrsn
- Date:
- Tue Aug 24 07:06:27 2021 +0000
- Revision:
- 1:2720d0e8b2f1
- Parent:
- 0:fe598340f74c
- Child:
- 2:626a20dd7987
Update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sgrsn | 0:fe598340f74c | 1 | #include "mbed.h" |
sgrsn | 0:fe598340f74c | 2 | #include "platform/mbed_thread.h" |
sgrsn | 0:fe598340f74c | 3 | #include "i2cmaster.h" |
sgrsn | 0:fe598340f74c | 4 | #include "mecanum_interface.hpp" |
sgrsn | 0:fe598340f74c | 5 | #include "imu_interface.hpp" |
sgrsn | 0:fe598340f74c | 6 | #include "mecanum_controller.hpp" |
sgrsn | 0:fe598340f74c | 7 | #include "waypoint_generator.hpp" |
sgrsn | 0:fe598340f74c | 8 | #include "ususama_serial.h" |
sgrsn | 0:fe598340f74c | 9 | #include "ususama_controller.hpp" |
sgrsn | 0:fe598340f74c | 10 | #include <vector> |
sgrsn | 0:fe598340f74c | 11 | #include <list> |
sgrsn | 0:fe598340f74c | 12 | |
sgrsn | 0:fe598340f74c | 13 | |
sgrsn | 0:fe598340f74c | 14 | i2c master(p28, p27); |
sgrsn | 0:fe598340f74c | 15 | MecanumI2C mecanum(&master); |
sgrsn | 0:fe598340f74c | 16 | PMSUInterface pmsu(p9, p10); |
sgrsn | 0:fe598340f74c | 17 | MecanumController ususama(&mecanum, &pmsu); |
sgrsn | 0:fe598340f74c | 18 | |
sgrsn | 0:fe598340f74c | 19 | int32_t Register[12] = {}; |
sgrsn | 0:fe598340f74c | 20 | UsusamaSerial pc(USBTX, USBRX, Register, 115200); |
sgrsn | 0:fe598340f74c | 21 | UsusamaController commander(&pc); |
sgrsn | 0:fe598340f74c | 22 | BusOut leds(LED1, LED2, LED3, LED4); |
sgrsn | 0:fe598340f74c | 23 | |
sgrsn | 0:fe598340f74c | 24 | Thread thread_read2pc; |
sgrsn | 0:fe598340f74c | 25 | Thread thread_write2pc; |
sgrsn | 0:fe598340f74c | 26 | |
sgrsn | 0:fe598340f74c | 27 | |
sgrsn | 0:fe598340f74c | 28 | void mecanum_test() |
sgrsn | 0:fe598340f74c | 29 | { |
sgrsn | 0:fe598340f74c | 30 | ususama.ControllVelocity(Vel2D(0.1, 0.0, 0.0)); |
sgrsn | 0:fe598340f74c | 31 | thread_sleep_for(1000); |
sgrsn | 0:fe598340f74c | 32 | ususama.ControllVelocity(Vel2D(0.0, 0.0, 0.0)); |
sgrsn | 0:fe598340f74c | 33 | } |
sgrsn | 0:fe598340f74c | 34 | |
sgrsn | 0:fe598340f74c | 35 | void imu_test() |
sgrsn | 0:fe598340f74c | 36 | { |
sgrsn | 0:fe598340f74c | 37 | for(int i = 0; i < 1000; i++) |
sgrsn | 0:fe598340f74c | 38 | { |
sgrsn | 0:fe598340f74c | 39 | printf("%d\r\n", (int)(pmsu.GetYawRadians() * RAD_TO_DEG)); |
sgrsn | 0:fe598340f74c | 40 | } |
sgrsn | 0:fe598340f74c | 41 | } |
sgrsn | 0:fe598340f74c | 42 | |
sgrsn | 0:fe598340f74c | 43 | void read_pc_thread() |
sgrsn | 0:fe598340f74c | 44 | { |
sgrsn | 0:fe598340f74c | 45 | while (true) { |
sgrsn | 0:fe598340f74c | 46 | commander.receive_pc_process(); |
sgrsn | 1:2720d0e8b2f1 | 47 | leds = commander.getReferencePose().x; |
sgrsn | 0:fe598340f74c | 48 | ThisThread::sleep_for(53); |
sgrsn | 0:fe598340f74c | 49 | } |
sgrsn | 0:fe598340f74c | 50 | } |
sgrsn | 0:fe598340f74c | 51 | |
sgrsn | 0:fe598340f74c | 52 | void write_pc_thread() |
sgrsn | 0:fe598340f74c | 53 | { |
sgrsn | 1:2720d0e8b2f1 | 54 | Pose2D current_pose(0,0,0); |
sgrsn | 0:fe598340f74c | 55 | while (true) { |
sgrsn | 1:2720d0e8b2f1 | 56 | current_pose = ususama.GetPose(); |
sgrsn | 1:2720d0e8b2f1 | 57 | commander.write_robot_state(current_pose.x, current_pose.y, current_pose.theta); |
sgrsn | 0:fe598340f74c | 58 | ThisThread::sleep_for(47); |
sgrsn | 0:fe598340f74c | 59 | } |
sgrsn | 0:fe598340f74c | 60 | } |
sgrsn | 0:fe598340f74c | 61 | |
sgrsn | 0:fe598340f74c | 62 | |
sgrsn | 0:fe598340f74c | 63 | int main() |
sgrsn | 0:fe598340f74c | 64 | { |
sgrsn | 0:fe598340f74c | 65 | std::list<std::unique_ptr<Pose2D>> ref_pose_list; |
sgrsn | 0:fe598340f74c | 66 | ref_pose_list.push_back(std::make_unique<Pose2D>(0.0, 0.8, 0.0)); |
sgrsn | 0:fe598340f74c | 67 | ref_pose_list.push_back(std::make_unique<Pose2D>(0.0, 0.0, 0.0)); |
sgrsn | 0:fe598340f74c | 68 | //ref_pose_list.push_back(std::make_unique<Pose2D>(0.0, 0.5, 0)); |
sgrsn | 0:fe598340f74c | 69 | //ref_pose_list.push_back(std::make_unique<Pose2D>(0.0, 0.0, -1.57)); |
sgrsn | 0:fe598340f74c | 70 | auto ref_pose_itr = ref_pose_list.begin(); |
sgrsn | 0:fe598340f74c | 71 | |
sgrsn | 0:fe598340f74c | 72 | // theta... |
sgrsn | 0:fe598340f74c | 73 | WaypointGenerator<Pose2D> way_generator( Pose2D(0, 0, 0), *(*ref_pose_itr), Pose2D(0.01, 0.01, 0.01), 5.0); |
sgrsn | 0:fe598340f74c | 74 | int i_way = 0; |
sgrsn | 0:fe598340f74c | 75 | Timer t_way; |
sgrsn | 0:fe598340f74c | 76 | t_way.start(); |
sgrsn | 0:fe598340f74c | 77 | |
sgrsn | 0:fe598340f74c | 78 | thread_read2pc.start(read_pc_thread); |
sgrsn | 0:fe598340f74c | 79 | thread_write2pc.start(write_pc_thread); |
sgrsn | 0:fe598340f74c | 80 | |
sgrsn | 0:fe598340f74c | 81 | while(1) |
sgrsn | 0:fe598340f74c | 82 | { |
sgrsn | 0:fe598340f74c | 83 | // todo: read using timer |
sgrsn | 0:fe598340f74c | 84 | thread_sleep_for(10); |
sgrsn | 0:fe598340f74c | 85 | |
sgrsn | 1:2720d0e8b2f1 | 86 | |
sgrsn | 1:2720d0e8b2f1 | 87 | UsusamaProtocol::MoveCommand_t ref_pose = commander.getReferencePose(); |
sgrsn | 1:2720d0e8b2f1 | 88 | |
sgrsn | 1:2720d0e8b2f1 | 89 | |
sgrsn | 0:fe598340f74c | 90 | ususama.ComputePose(); |
sgrsn | 0:fe598340f74c | 91 | ususama.ControlPosition( way_generator.GetPose(i_way) ); |
sgrsn | 0:fe598340f74c | 92 | |
sgrsn | 0:fe598340f74c | 93 | if(way_generator.GetPose(i_way).time_stamp < t_way.read()) |
sgrsn | 0:fe598340f74c | 94 | { |
sgrsn | 0:fe598340f74c | 95 | i_way++; |
sgrsn | 0:fe598340f74c | 96 | } |
sgrsn | 0:fe598340f74c | 97 | |
sgrsn | 0:fe598340f74c | 98 | if( ususama.IsReferencePose( *(*ref_pose_itr) ) ) |
sgrsn | 0:fe598340f74c | 99 | { |
sgrsn | 0:fe598340f74c | 100 | //printf("goal\r\n"); |
sgrsn | 0:fe598340f74c | 101 | |
sgrsn | 0:fe598340f74c | 102 | ref_pose_itr++; |
sgrsn | 0:fe598340f74c | 103 | if(ref_pose_itr == ref_pose_list.end()) |
sgrsn | 0:fe598340f74c | 104 | { |
sgrsn | 0:fe598340f74c | 105 | ref_pose_itr = ref_pose_list.begin(); |
sgrsn | 0:fe598340f74c | 106 | } |
sgrsn | 0:fe598340f74c | 107 | |
sgrsn | 0:fe598340f74c | 108 | // update waypoint |
sgrsn | 0:fe598340f74c | 109 | //printf("generate new waypoint..."); |
sgrsn | 0:fe598340f74c | 110 | way_generator = WaypointGenerator<Pose2D>( ususama.GetPose(), *(*ref_pose_itr), Pose2D(0.01, 0.01, 0.005), 5.0); |
sgrsn | 0:fe598340f74c | 111 | //printf("has finished\r\n"); |
sgrsn | 0:fe598340f74c | 112 | |
sgrsn | 0:fe598340f74c | 113 | i_way = 0; |
sgrsn | 0:fe598340f74c | 114 | t_way.reset(); |
sgrsn | 0:fe598340f74c | 115 | t_way.start(); |
sgrsn | 0:fe598340f74c | 116 | } |
sgrsn | 0:fe598340f74c | 117 | } |
sgrsn | 0:fe598340f74c | 118 | } |