blob: 3b80184f987199fe5af825609e52a1a238611b59 [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) {
78 DoFridgeProfile(params.bottom + values.tote_height, 0.0, kFastElevatorMove,
79 kFastArmMove, false);
80 // Finish early if we aren't supposed to grab.
81 return true;
82 }
83
84 DoFridgeProfile(params.bottom + values.tote_height, params.arm_clearance,
85 kFastElevatorMove, kFastArmMove, false);
86
87 if (ShouldCancel()) return true;
88 DoFridgeProfile(params.bottom, params.arm_clearance, kFastElevatorMove,
89 kFastArmMove, false);
Austin Schuhd2f47fc2015-03-01 00:06:27 -080090 if (ShouldCancel()) return true;
Austin Schuhb53df1a2015-03-15 00:57:08 -070091 DoFridgeProfile(params.bottom, 0.0, kFastElevatorMove, kFastArmMove, false);
Austin Schuhd2f47fc2015-03-01 00:06:27 -080092 if (ShouldCancel()) return true;
Austin Schuhb53df1a2015-03-15 00:57:08 -070093 aos::time::SleepFor(aos::time::Time::InMS(200));
Daniel Pettidfc90ba2015-02-17 21:42:15 -080094
95 return true;
96}
97
Austin Schuhdc171012015-02-22 21:36:55 -080098::std::unique_ptr<StackAction> MakeStackAction(const StackParams &params) {
Daniel Pettidfc90ba2015-02-17 21:42:15 -080099 return ::std::unique_ptr<StackAction>(
Austin Schuhdc171012015-02-22 21:36:55 -0800100 new StackAction(&::frc971::actors::stack_action, params));
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800101}
102
103} // namespace actors
104} // namespace frc971