Mecanum robot firmware for WRS2020 Ususama
Dependencies: UsusamaSerial MecanumInterface PMSU_100 PIDController IMUInterface WaypointManager i2cmaster MecanumController
main.cpp@3:35244995ff2e, 2021-08-25 (annotated)
- Committer:
- sgrsn
- Date:
- Wed Aug 25 12:12:06 2021 +0000
- Revision:
- 3:35244995ff2e
- Parent:
- 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 | 2:626a20dd7987 | 30 | ususama.ControllVelocity(Vel2D(1.0, 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 | 3:35244995ff2e | 47 | ThisThread::sleep_for(103); |
sgrsn | 0:fe598340f74c | 48 | } |
sgrsn | 0:fe598340f74c | 49 | } |
sgrsn | 0:fe598340f74c | 50 | |
sgrsn | 0:fe598340f74c | 51 | void write_pc_thread() |
sgrsn | 0:fe598340f74c | 52 | { |
sgrsn | 0:fe598340f74c | 53 | while (true) { |
sgrsn | 2:626a20dd7987 | 54 | commander.write_robot_state(); |
sgrsn | 3:35244995ff2e | 55 | ThisThread::sleep_for(211); |
sgrsn | 0:fe598340f74c | 56 | } |
sgrsn | 0:fe598340f74c | 57 | } |
sgrsn | 0:fe598340f74c | 58 | |
sgrsn | 0:fe598340f74c | 59 | |
sgrsn | 0:fe598340f74c | 60 | int main() |
sgrsn | 0:fe598340f74c | 61 | { |
sgrsn | 2:626a20dd7987 | 62 | //std::list<std::unique_ptr<Pose2D>> ref_pose_list; |
sgrsn | 2:626a20dd7987 | 63 | //ref_pose_list.push_back(std::make_unique<Pose2D>(0.0, 0.8, 0.0)); |
sgrsn | 2:626a20dd7987 | 64 | //ref_pose_list.push_back(std::make_unique<Pose2D>(0.0, 0.0, 0.0)); |
sgrsn | 0:fe598340f74c | 65 | //ref_pose_list.push_back(std::make_unique<Pose2D>(0.0, 0.5, 0)); |
sgrsn | 0:fe598340f74c | 66 | //ref_pose_list.push_back(std::make_unique<Pose2D>(0.0, 0.0, -1.57)); |
sgrsn | 2:626a20dd7987 | 67 | //auto ref_pose_itr = ref_pose_list.begin(); |
sgrsn | 0:fe598340f74c | 68 | |
sgrsn | 2:626a20dd7987 | 69 | ususama.ControllVelocity(Vel2D(0.0, 0.0, 0.0)); |
sgrsn | 0:fe598340f74c | 70 | // theta... |
sgrsn | 2:626a20dd7987 | 71 | WaypointGenerator<Pose2D> way_generator( Pose2D(0, 0, 0), Pose2D(0.8, 0, 0), 10.0); |
sgrsn | 0:fe598340f74c | 72 | Timer t_way; |
sgrsn | 0:fe598340f74c | 73 | t_way.start(); |
sgrsn | 0:fe598340f74c | 74 | |
sgrsn | 0:fe598340f74c | 75 | thread_read2pc.start(read_pc_thread); |
sgrsn | 0:fe598340f74c | 76 | thread_write2pc.start(write_pc_thread); |
sgrsn | 0:fe598340f74c | 77 | |
sgrsn | 2:626a20dd7987 | 78 | // to do waypoint生成とmove_command.enableの処理 |
sgrsn | 2:626a20dd7987 | 79 | bool is_way_generated = false; |
sgrsn | 2:626a20dd7987 | 80 | bool is_stoped = false; |
sgrsn | 3:35244995ff2e | 81 | bool new_goal = false; |
sgrsn | 2:626a20dd7987 | 82 | |
sgrsn | 0:fe598340f74c | 83 | while(1) |
sgrsn | 0:fe598340f74c | 84 | { |
sgrsn | 2:626a20dd7987 | 85 | //thread_sleep_for(10); |
sgrsn | 1:2720d0e8b2f1 | 86 | |
sgrsn | 0:fe598340f74c | 87 | ususama.ComputePose(); |
sgrsn | 2:626a20dd7987 | 88 | commander.update(ususama.GetPose().x, ususama.GetPose().y, ususama.GetPose().theta); |
sgrsn | 2:626a20dd7987 | 89 | |
sgrsn | 2:626a20dd7987 | 90 | UsusamaProtocol::MoveCommand_t move_command = commander.getMoveCommand(); |
sgrsn | 2:626a20dd7987 | 91 | Pose2D ref_pose(move_command.x, move_command.y, move_command.theta); |
sgrsn | 0:fe598340f74c | 92 | |
sgrsn | 2:626a20dd7987 | 93 | if(!move_command.enable) |
sgrsn | 0:fe598340f74c | 94 | { |
sgrsn | 2:626a20dd7987 | 95 | ususama.ControllVelocity(Vel2D(0.0, 0.0, 0.0)); |
sgrsn | 2:626a20dd7987 | 96 | continue; |
sgrsn | 0:fe598340f74c | 97 | } |
sgrsn | 0:fe598340f74c | 98 | |
sgrsn | 3:35244995ff2e | 99 | if( commander.is_ref_pose_changed() || !is_way_generated) |
sgrsn | 2:626a20dd7987 | 100 | { |
sgrsn | 2:626a20dd7987 | 101 | way_generator = WaypointGenerator<Pose2D>( |
sgrsn | 2:626a20dd7987 | 102 | ususama.GetPose(), |
sgrsn | 2:626a20dd7987 | 103 | ref_pose, |
sgrsn | 2:626a20dd7987 | 104 | 10.0 |
sgrsn | 2:626a20dd7987 | 105 | ); |
sgrsn | 2:626a20dd7987 | 106 | is_way_generated = true; |
sgrsn | 3:35244995ff2e | 107 | commander.notify_use_ref_pose(); |
sgrsn | 0:fe598340f74c | 108 | t_way.reset(); |
sgrsn | 0:fe598340f74c | 109 | t_way.start(); |
sgrsn | 0:fe598340f74c | 110 | } |
sgrsn | 3:35244995ff2e | 111 | // waypoint存在していれば移動 |
sgrsn | 3:35244995ff2e | 112 | else |
sgrsn | 3:35244995ff2e | 113 | { |
sgrsn | 3:35244995ff2e | 114 | float t = t_way.read(); |
sgrsn | 3:35244995ff2e | 115 | ususama.ControlPosition( way_generator.GetPose( t ) ); |
sgrsn | 3:35244995ff2e | 116 | } |
sgrsn | 2:626a20dd7987 | 117 | // stopコマンド確認 |
sgrsn | 2:626a20dd7987 | 118 | if( commander.is_stop_called() ) |
sgrsn | 2:626a20dd7987 | 119 | { |
sgrsn | 2:626a20dd7987 | 120 | is_way_generated = false; |
sgrsn | 2:626a20dd7987 | 121 | commander.disable_to_move(); |
sgrsn | 2:626a20dd7987 | 122 | t_way.stop(); |
sgrsn | 2:626a20dd7987 | 123 | is_stoped = true; |
sgrsn | 2:626a20dd7987 | 124 | //ususama.ControllVelocity( Vel2D(0, 0, 0) ); |
sgrsn | 2:626a20dd7987 | 125 | // ususama.CoastAllMotor(); |
sgrsn | 2:626a20dd7987 | 126 | // ususama.ControlPosition( ususama.GetPose() ); |
sgrsn | 2:626a20dd7987 | 127 | } |
sgrsn | 2:626a20dd7987 | 128 | else if(is_stoped) |
sgrsn | 2:626a20dd7987 | 129 | { |
sgrsn | 2:626a20dd7987 | 130 | t_way.start(); |
sgrsn | 2:626a20dd7987 | 131 | is_stoped = false; |
sgrsn | 2:626a20dd7987 | 132 | } |
sgrsn | 2:626a20dd7987 | 133 | |
sgrsn | 2:626a20dd7987 | 134 | |
sgrsn | 2:626a20dd7987 | 135 | if( ususama.IsReferencePose( ref_pose ) ) |
sgrsn | 2:626a20dd7987 | 136 | { |
sgrsn | 2:626a20dd7987 | 137 | is_way_generated = false; |
sgrsn | 2:626a20dd7987 | 138 | commander.disable_to_move(); |
sgrsn | 2:626a20dd7987 | 139 | commander.setReachedGoal(true); |
sgrsn | 2:626a20dd7987 | 140 | } |
sgrsn | 2:626a20dd7987 | 141 | else |
sgrsn | 2:626a20dd7987 | 142 | { |
sgrsn | 2:626a20dd7987 | 143 | commander.setReachedGoal(false); |
sgrsn | 2:626a20dd7987 | 144 | } |
sgrsn | 0:fe598340f74c | 145 | } |
sgrsn | 0:fe598340f74c | 146 | } |