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