Brian Silverman | b691f5e | 2015-08-02 11:37:55 -0700 | [diff] [blame] | 1 | #include "y2015/actors/pickup_actor.h" |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 2 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 3 | #include <chrono> |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame] | 4 | #include <cmath> |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 5 | |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 6 | #include "aos/common/controls/control_loop.h" |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 7 | #include "aos/common/logging/logging.h" |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 8 | #include "aos/common/time.h" |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 9 | #include "aos/common/util/phased_loop.h" |
Brian Silverman | b691f5e | 2015-08-02 11:37:55 -0700 | [diff] [blame] | 10 | #include "y2015/control_loops/claw/claw.q.h" |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 11 | |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 12 | namespace y2015 { |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 13 | namespace actors { |
Austin Schuh | e4e59ef | 2015-03-01 00:05:37 -0800 | [diff] [blame] | 14 | namespace { |
| 15 | constexpr double kClawPickupVelocity = 3.00; |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 16 | constexpr double kClawPickupAcceleration = 3.5; |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 17 | constexpr double kClawMoveDownVelocity = 7.00; |
| 18 | constexpr double kClawMoveDownAcceleration = 15.0; |
| 19 | constexpr double kClawMoveUpVelocity = 8.0; |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 20 | constexpr double kClawMoveUpAcceleration = 25.0; |
Austin Schuh | e4e59ef | 2015-03-01 00:05:37 -0800 | [diff] [blame] | 21 | } // namespace |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 22 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 23 | namespace chrono = ::std::chrono; |
| 24 | using ::aos::monotonic_clock; |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 25 | using ::y2015::control_loops::claw_queue; |
| 26 | |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 27 | PickupActor::PickupActor(PickupActionQueueGroup* queues) |
| 28 | : aos::common::actions::ActorBase<PickupActionQueueGroup>(queues) {} |
| 29 | |
| 30 | bool PickupActor::RunAction(const PickupParams& params) { |
Austin Schuh | e4e59ef | 2015-03-01 00:05:37 -0800 | [diff] [blame] | 31 | constexpr double kAngleEpsilon = 0.10; |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 32 | // Start lifting the tote. |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 33 | { |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 34 | auto message = claw_queue.goal.MakeMessage(); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 35 | message->angle = params.pickup_angle; |
Austin Schuh | e4e59ef | 2015-03-01 00:05:37 -0800 | [diff] [blame] | 36 | message->max_velocity = kClawPickupVelocity; |
| 37 | message->max_acceleration = kClawPickupAcceleration; |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 38 | message->angular_velocity = 0.0; |
| 39 | message->intake = 0.0; |
| 40 | message->rollers_closed = true; |
| 41 | |
| 42 | LOG_STRUCT(DEBUG, "Sending claw goal", *message); |
| 43 | message.Send(); |
| 44 | } |
| 45 | while (true) { |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 46 | claw_queue.status.FetchAnother(); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 47 | if (ShouldCancel()) return true; |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 48 | const double current_angle = claw_queue.status->angle; |
| 49 | LOG_STRUCT(DEBUG, "Got claw status", *claw_queue.status); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 50 | |
| 51 | if (current_angle > params.suck_angle || |
| 52 | ::std::abs(current_angle - params.pickup_angle) < kAngleEpsilon) { |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 57 | // Once above params.suck_angle, start sucking while lifting. |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 58 | { |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 59 | auto message = claw_queue.goal.MakeMessage(); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 60 | message->angle = params.pickup_angle; |
Austin Schuh | e4e59ef | 2015-03-01 00:05:37 -0800 | [diff] [blame] | 61 | message->max_velocity = kClawPickupVelocity; |
| 62 | message->max_acceleration = kClawPickupAcceleration; |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 63 | message->angular_velocity = 0.0; |
| 64 | message->intake = params.intake_voltage; |
| 65 | message->rollers_closed = true; |
| 66 | |
| 67 | LOG_STRUCT(DEBUG, "Sending claw goal", *message); |
| 68 | message.Send(); |
| 69 | } |
| 70 | |
| 71 | while (true) { |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 72 | claw_queue.status.FetchAnother(); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 73 | if (ShouldCancel()) return true; |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 74 | const double current_angle = claw_queue.status->angle; |
| 75 | LOG_STRUCT(DEBUG, "Got claw status", *claw_queue.status); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 76 | |
| 77 | if (::std::abs(current_angle - params.pickup_angle) < kAngleEpsilon) { |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 82 | // Now that we have reached the upper height, come back down while intaking. |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 83 | { |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 84 | auto message = claw_queue.goal.MakeMessage(); |
Austin Schuh | e4e59ef | 2015-03-01 00:05:37 -0800 | [diff] [blame] | 85 | message->angle = params.suck_angle_finish; |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 86 | message->max_velocity = kClawMoveDownVelocity; |
| 87 | message->max_acceleration = kClawMoveDownAcceleration; |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 88 | message->angular_velocity = 0.0; |
| 89 | message->intake = params.intake_voltage; |
| 90 | message->rollers_closed = true; |
| 91 | |
| 92 | LOG_STRUCT(DEBUG, "Sending claw goal", *message); |
| 93 | message.Send(); |
| 94 | } |
| 95 | |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 96 | // Pull in for params.intake_time. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 97 | monotonic_clock::time_point end_time = |
| 98 | monotonic_clock::now() + |
| 99 | chrono::duration_cast<chrono::nanoseconds>( |
| 100 | chrono::duration<double>(params.intake_time)); |
| 101 | ::aos::time::PhasedLoop phased_loop(::aos::controls::kLoopFrequency, |
| 102 | ::std::chrono::milliseconds(5) / 2); |
| 103 | while (monotonic_clock::now() <= end_time) { |
| 104 | phased_loop.SleepUntilNext(); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 105 | if (ShouldCancel()) return true; |
| 106 | } |
| 107 | |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 108 | // Lift the claw back up to pack the box back in. |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 109 | { |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 110 | auto message = claw_queue.goal.MakeMessage(); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 111 | message->angle = params.pickup_finish_angle; |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 112 | message->max_velocity = kClawMoveUpVelocity; |
| 113 | message->max_acceleration = kClawMoveUpAcceleration; |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 114 | message->angular_velocity = 0.0; |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 115 | message->intake = params.intake_voltage; |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 116 | message->rollers_closed = true; |
| 117 | |
| 118 | LOG_STRUCT(DEBUG, "Sending claw goal", *message); |
| 119 | message.Send(); |
| 120 | } |
| 121 | |
| 122 | while (true) { |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 123 | claw_queue.status.FetchAnother(); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 124 | if (ShouldCancel()) return true; |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 125 | const double current_angle = claw_queue.status->angle; |
| 126 | LOG_STRUCT(DEBUG, "Got claw status", *claw_queue.status); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 127 | |
| 128 | if (::std::abs(current_angle - params.pickup_finish_angle) < |
| 129 | kAngleEpsilon) { |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 134 | // Stop the motors... |
| 135 | { |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 136 | auto message = claw_queue.goal.MakeMessage(); |
Austin Schuh | a2ea71a | 2015-04-18 22:55:28 -0700 | [diff] [blame] | 137 | message->angle = params.pickup_finish_angle; |
| 138 | message->max_velocity = kClawMoveUpVelocity; |
| 139 | message->max_acceleration = kClawMoveUpAcceleration; |
| 140 | message->angular_velocity = 0.0; |
| 141 | message->intake = 0.0; |
| 142 | message->rollers_closed = true; |
| 143 | |
| 144 | LOG_STRUCT(DEBUG, "Sending claw goal", *message); |
| 145 | message.Send(); |
| 146 | } |
| 147 | |
| 148 | |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
| 152 | ::std::unique_ptr<PickupAction> MakePickupAction(const PickupParams& params) { |
| 153 | return ::std::unique_ptr<PickupAction>( |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 154 | new PickupAction(&::y2015::actors::pickup_action, params)); |
Austin Schuh | 6ab08b8 | 2015-02-22 21:41:05 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } // namespace actors |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 158 | } // namespace y2015 |