blob: cf5766c623b36d8fbdcb516b7803c185a88621e7 [file] [log] [blame]
Austin Schuha3c148e2018-03-09 21:04:05 -08001#ifndef Y2018_ACTORS_AUTONOMOUS_ACTOR_H_
2#define Y2018_ACTORS_AUTONOMOUS_ACTOR_H_
3
4#include <chrono>
5#include <memory>
6
7#include "aos/common/actions/actions.h"
8#include "aos/common/actions/actor.h"
9#include "frc971/autonomous/base_autonomous_actor.h"
10#include "frc971/control_loops/drivetrain/drivetrain.q.h"
11#include "frc971/control_loops/drivetrain/drivetrain_config.h"
12#include "y2018/control_loops/superstructure/arm/generated_graph.h"
13#include "y2018/control_loops/superstructure/superstructure.q.h"
14
15namespace y2018 {
16namespace actors {
17using ::y2018::control_loops::superstructure_queue;
18
19namespace arm = ::y2018::control_loops::superstructure::arm;
20
21class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
22 public:
23 explicit AutonomousActor(::frc971::autonomous::AutonomousActionQueueGroup *s);
24
25 bool RunAction(
26 const ::frc971::autonomous::AutonomousActionParams &params) override;
27 private:
28 void Reset() {
29 roller_voltage_ = 0.0;
30 left_intake_angle_ = -3.3;
31 right_intake_angle_ = -3.3;
32 arm_goal_position_ = arm::NeutralIndex();
33 grab_box_ = false;
34 open_claw_ = false;
35 deploy_fork_ = false;
36 InitializeEncoders();
37 ResetDrivetrain();
38 SendSuperstructureGoal();
39 }
40
41 double roller_voltage_ = 0.0;
42 double left_intake_angle_ = -3.3;
43 double right_intake_angle_ = -3.3;
44 uint32_t arm_goal_position_ = arm::NeutralIndex();
45 bool grab_box_ = false;
46 bool open_claw_ = false;
47 bool deploy_fork_ = false;
48
49 void set_roller_voltage(double roller_voltage) {
50 roller_voltage_ = roller_voltage;
51 }
52 void set_left_intake_angle(double left_intake_angle) {
53 left_intake_angle_ = left_intake_angle;
54 }
55 void set_right_intake_angle(double right_intake_angle) {
56 right_intake_angle_ = right_intake_angle;
57 }
58 void set_arm_goal_position(uint32_t arm_goal_position) {
59 arm_goal_position_ = arm_goal_position;
60 }
61 void set_grab_box(bool grab_box) { grab_box_ = grab_box; }
62 void set_open_claw(bool open_claw) { open_claw_ = open_claw; }
63 void set_deploy_fork(bool deploy_fork) { deploy_fork_ = deploy_fork; }
64
65 void SendSuperstructureGoal() {
66 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
67 new_superstructure_goal->intake.roller_voltage = roller_voltage_;
68 new_superstructure_goal->intake.left_intake_angle = left_intake_angle_;
69 new_superstructure_goal->intake.right_intake_angle = right_intake_angle_;
70
71 new_superstructure_goal->arm_goal_position = arm_goal_position_;
72 new_superstructure_goal->grab_box = grab_box_;
73 new_superstructure_goal->open_claw = open_claw_;
74 new_superstructure_goal->deploy_fork = deploy_fork_;
75
76 if (!new_superstructure_goal.Send()) {
77 LOG(ERROR, "Sending superstructure goal failed.\n");
78 }
79 }
Austin Schuhc231df42018-03-21 20:43:24 -070080
81 bool WaitForArmTrajectoryClose(double threshold) {
82 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
83 ::std::chrono::milliseconds(5) / 2);
84 while (true) {
85 if (ShouldCancel()) {
86 return false;
87 }
88
89 superstructure_queue.status.FetchLatest();
90 if (superstructure_queue.status.get()) {
91 if (superstructure_queue.status->arm.current_node == arm_goal_position_ &&
92 superstructure_queue.status->arm.path_distance_to_go < threshold) {
93 return true;
94 }
95 }
96 phased_loop.SleepUntilNext();
97 }
98 }
Austin Schuha3c148e2018-03-09 21:04:05 -080099};
100
101} // namespace actors
102} // namespace y2018
103
104#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_