blob: 49fbf98165647bd9be68876c6c1cfc5be3141208 [file] [log] [blame]
Austin Schuh6e242ac2015-03-07 17:08:21 -08001#include "frc971/actors/stack_actor.h"
2
Daniel Pettidfc90ba2015-02-17 21:42:15 -08003#include <math.h>
4
Austin Schuhdc171012015-02-22 21:36:55 -08005#include "aos/common/time.h"
Ben Fredrickson32e6c252015-02-21 23:53:38 -08006#include "aos/common/util/phased_loop.h"
7
Daniel Pettidfc90ba2015-02-17 21:42:15 -08008#include "frc971/constants.h"
Austin Schuhdc171012015-02-22 21:36:55 -08009#include "frc971/control_loops/claw/claw.q.h"
Daniel Pettidfc90ba2015-02-17 21:42:15 -080010
11namespace frc971 {
12namespace actors {
13namespace {
Austin Schuh85bda502015-03-15 19:57:27 -070014constexpr ProfileParams kArmWithStackMove{1.75, 0.60};
Austin Schuhb53df1a2015-03-15 00:57:08 -070015constexpr ProfileParams kSlowArmMove{1.3, 1.4};
Austin Schuh6e242ac2015-03-07 17:08:21 -080016constexpr ProfileParams kSlowElevatorMove{0.5, 3.0};
Daniel Pettidfc90ba2015-02-17 21:42:15 -080017
Austin Schuh6e242ac2015-03-07 17:08:21 -080018constexpr ProfileParams kFastArmMove{0.8, 4.0};
19constexpr ProfileParams kFastElevatorMove{1.2, 5.0};
Daniel Pettidfc90ba2015-02-17 21:42:15 -080020} // namespace
21
Austin Schuhdc171012015-02-22 21:36:55 -080022StackActor::StackActor(StackActionQueueGroup *queues)
Austin Schuh6e242ac2015-03-07 17:08:21 -080023 : FridgeActorBase<StackActionQueueGroup>(queues) {}
Daniel Pettidfc90ba2015-02-17 21:42:15 -080024
Austin Schuhdc171012015-02-22 21:36:55 -080025bool StackActor::RunAction(const StackParams &params) {
26 const auto &values = constants::GetValues();
Daniel Pettidfc90ba2015-02-17 21:42:15 -080027
Austin Schuh85bda502015-03-15 19:57:27 -070028 control_loops::fridge_queue.status.FetchLatest();
29 if (!control_loops::fridge_queue.status.get()) {
30 LOG(ERROR, "Got no fridge status packet.\n");
31 return false;
32 }
33
34 // If we are really high, probably have a can. Move over before down.
35 if (control_loops::fridge_queue.status->goal_height >
36 params.over_box_before_place_height + 0.1) {
37 // Set the current stack down on top of the bottom box.
38 DoFridgeProfile(control_loops::fridge_queue.status->goal_height, 0.0,
39 kSlowElevatorMove, kArmWithStackMove, true);
40 if (ShouldCancel()) return true;
41 }
42
Daniel Pettidfc90ba2015-02-17 21:42:15 -080043 // Set the current stack down on top of the bottom box.
Austin Schuhb53df1a2015-03-15 00:57:08 -070044 DoFridgeProfile(params.over_box_before_place_height, 0.0, kSlowElevatorMove,
Austin Schuh85bda502015-03-15 19:57:27 -070045 kArmWithStackMove, true);
Ben Fredrickson32e6c252015-02-21 23:53:38 -080046 if (ShouldCancel()) return true;
Austin Schuh6e242ac2015-03-07 17:08:21 -080047 // Set down on the box.
Austin Schuhb53df1a2015-03-15 00:57:08 -070048 DoFridgeProfile(params.bottom + values.tote_height, 0.0, kSlowElevatorMove,
49 kSlowArmMove, true);
Ben Fredrickson32e6c252015-02-21 23:53:38 -080050 if (ShouldCancel()) return true;
Austin Schuh85bda502015-03-15 19:57:27 -070051
Daniel Pettidfc90ba2015-02-17 21:42:15 -080052 // Clamp.
Austin Schuh85bda502015-03-15 19:57:27 -070053 if (!params.only_place) {
54 // Move the claw out of the way only if we are supposed to pick up.
Austin Schuh813b9af2015-03-08 18:46:58 -070055 bool send_goal = true;
56 control_loops::claw_queue.status.FetchLatest();
57 if (control_loops::claw_queue.status.get()) {
58 if (control_loops::claw_queue.status->goal_angle <
59 params.claw_out_angle) {
60 send_goal = false;
61 }
62 }
63 if (send_goal) {
64 auto message = control_loops::claw_queue.goal.MakeMessage();
65 message->angle = params.claw_out_angle;
66 message->angular_velocity = 0.0;
67 message->intake = 0.0;
68 message->rollers_closed = true;
69 message->max_velocity = 6.0;
70 message->max_acceleration = 10.0;
Austin Schuhdc171012015-02-22 21:36:55 -080071
Austin Schuh813b9af2015-03-08 18:46:58 -070072 LOG_STRUCT(DEBUG, "Sending claw goal", *message);
73 message.Send();
74 }
Austin Schuhdc171012015-02-22 21:36:55 -080075 }
Austin Schuh85bda502015-03-15 19:57:27 -070076
77 if (params.only_place) {
Ben Fredricksone2b1dd12015-03-27 21:18:08 -070078 // open grabber for place only
Austin Schuh85bda502015-03-15 19:57:27 -070079 DoFridgeProfile(params.bottom + values.tote_height, 0.0, kFastElevatorMove,
80 kFastArmMove, false);
81 // Finish early if we aren't supposed to grab.
82 return true;
83 }
84
Ben Fredricksone2b1dd12015-03-27 21:18:08 -070085 // TODO(ben): I'm not sure why this liitle jog is here. we are removing it for
86 // the fangs, but I want to keep the code here so austin can explain.
87 /*DoFridgeProfile(params.bottom + values.tote_height, params.arm_clearance,
Austin Schuh85bda502015-03-15 19:57:27 -070088 kFastElevatorMove, kFastArmMove, false);
89
90 if (ShouldCancel()) return true;
91 DoFridgeProfile(params.bottom, params.arm_clearance, kFastElevatorMove,
Ben Fredricksone2b1dd12015-03-27 21:18:08 -070092 kFastArmMove, false);*/
Austin Schuhd2f47fc2015-03-01 00:06:27 -080093 if (ShouldCancel()) return true;
Ben Fredricksone2b1dd12015-03-27 21:18:08 -070094 // grab can (if in fang mode the grabber stays closed)
95 DoFridgeProfile(params.bottom, 0.0, kFastElevatorMove, kFastArmMove, false,
96 true, true);
Austin Schuhd2f47fc2015-03-01 00:06:27 -080097 if (ShouldCancel()) return true;
Austin Schuhb53df1a2015-03-15 00:57:08 -070098 aos::time::SleepFor(aos::time::Time::InMS(200));
Daniel Pettidfc90ba2015-02-17 21:42:15 -080099
100 return true;
101}
102
Austin Schuhdc171012015-02-22 21:36:55 -0800103::std::unique_ptr<StackAction> MakeStackAction(const StackParams &params) {
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800104 return ::std::unique_ptr<StackAction>(
Austin Schuhdc171012015-02-22 21:36:55 -0800105 new StackAction(&::frc971::actors::stack_action, params));
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800106}
107
108} // namespace actors
109} // namespace frc971