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