blob: 20dc66a13552be35bc8787bb13be8d108a7b8c26 [file] [log] [blame]
Brian Silvermanb691f5e2015-08-02 11:37:55 -07001#include "y2015/actors/stack_actor.h"
Austin Schuh6e242ac2015-03-07 17:08:21 -08002
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
Brian Silvermanb691f5e2015-08-02 11:37:55 -07008#include "y2015/constants.h"
9#include "y2015/control_loops/claw/claw.q.h"
Daniel Pettidfc90ba2015-02-17 21:42:15 -080010
Austin Schuh88af0852016-12-04 20:31:32 -080011namespace y2015 {
Daniel Pettidfc90ba2015-02-17 21:42:15 -080012namespace actors {
13namespace {
Austin Schuh67efa4c2015-03-29 13:43:32 -070014constexpr ProfileParams kArmWithStackMove{1.75, 4.20};
Austin Schuhb53df1a2015-03-15 00:57:08 -070015constexpr ProfileParams kSlowArmMove{1.3, 1.4};
Austin Schuh67efa4c2015-03-29 13:43:32 -070016constexpr ProfileParams kSlowElevatorMove{1.0, 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};
Austin Schuh67efa4c2015-03-29 13:43:32 -070020constexpr ProfileParams kReallyFastElevatorMove{1.2, 6.0};
Daniel Pettidfc90ba2015-02-17 21:42:15 -080021} // namespace
22
Austin Schuh88af0852016-12-04 20:31:32 -080023using ::y2015::control_loops::claw_queue;
24using ::y2015::control_loops::fridge::fridge_queue;
25
Austin Schuhdc171012015-02-22 21:36:55 -080026StackActor::StackActor(StackActionQueueGroup *queues)
Austin Schuh6e242ac2015-03-07 17:08:21 -080027 : FridgeActorBase<StackActionQueueGroup>(queues) {}
Daniel Pettidfc90ba2015-02-17 21:42:15 -080028
Austin Schuhdc171012015-02-22 21:36:55 -080029bool StackActor::RunAction(const StackParams &params) {
30 const auto &values = constants::GetValues();
Daniel Pettidfc90ba2015-02-17 21:42:15 -080031
Austin Schuh88af0852016-12-04 20:31:32 -080032 fridge_queue.status.FetchLatest();
33 if (!fridge_queue.status.get()) {
Austin Schuh85bda502015-03-15 19:57:27 -070034 LOG(ERROR, "Got no fridge status packet.\n");
35 return false;
36 }
37
38 // If we are really high, probably have a can. Move over before down.
Austin Schuh88af0852016-12-04 20:31:32 -080039 if (fridge_queue.status->goal_height >
Austin Schuh85bda502015-03-15 19:57:27 -070040 params.over_box_before_place_height + 0.1) {
41 // Set the current stack down on top of the bottom box.
Austin Schuh88af0852016-12-04 20:31:32 -080042 DoFridgeProfile(fridge_queue.status->goal_height, 0.0,
Austin Schuh85bda502015-03-15 19:57:27 -070043 kSlowElevatorMove, kArmWithStackMove, true);
44 if (ShouldCancel()) return true;
45 }
46
Daniel Pettidfc90ba2015-02-17 21:42:15 -080047 // Set the current stack down on top of the bottom 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;
Austin Schuh88af0852016-12-04 20:31:32 -080056 claw_queue.status.FetchLatest();
57 if (claw_queue.status.get()) {
58 if (claw_queue.status->goal_angle <
Austin Schuh813b9af2015-03-08 18:46:58 -070059 params.claw_out_angle) {
60 send_goal = false;
61 }
62 }
63 if (send_goal) {
Austin Schuh88af0852016-12-04 20:31:32 -080064 auto message = claw_queue.goal.MakeMessage();
Austin Schuh813b9af2015-03-08 18:46:58 -070065 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
Austin Schuhd2f47fc2015-03-01 00:06:27 -080085 if (ShouldCancel()) return true;
Ben Fredricksone2b1dd12015-03-27 21:18:08 -070086 // grab can (if in fang mode the grabber stays closed)
Austin Schuh67efa4c2015-03-29 13:43:32 -070087 DoFridgeProfile(params.bottom, 0.0, kReallyFastElevatorMove, kFastArmMove, true,
Ben Fredricksone2b1dd12015-03-27 21:18:08 -070088 true, true);
Daniel Pettidfc90ba2015-02-17 21:42:15 -080089
90 return true;
91}
92
Austin Schuhdc171012015-02-22 21:36:55 -080093::std::unique_ptr<StackAction> MakeStackAction(const StackParams &params) {
Daniel Pettidfc90ba2015-02-17 21:42:15 -080094 return ::std::unique_ptr<StackAction>(
Austin Schuh88af0852016-12-04 20:31:32 -080095 new StackAction(&::y2015::actors::stack_action, params));
Daniel Pettidfc90ba2015-02-17 21:42:15 -080096}
97
98} // namespace actors
Austin Schuh88af0852016-12-04 20:31:32 -080099} // namespace y2015