blob: a9c0f8c754108702910913f6da53af8d0376ded8 [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include <math.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6#include "aos/actions/actions.h"
7#include "aos/init.h"
8#include "aos/input/action_joystick_input.h"
9#include "aos/input/driver_station_data.h"
10#include "aos/input/drivetrain_input.h"
11#include "aos/input/joystick_input.h"
12#include "aos/logging/logging.h"
13#include "aos/network/team_number.h"
14#include "aos/util/log_interval.h"
15#include "frc971/autonomous/base_autonomous_actor.h"
16#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Sabina Davisa8fed3d2020-02-22 21:44:57 -080017#include "y2020/constants.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080018#include "y2020/control_loops/superstructure/superstructure_goal_generated.h"
19#include "y2020/control_loops/superstructure/superstructure_status_generated.h"
20
21using aos::input::driver_station::ButtonLocation;
22using aos::input::driver_station::ControlBit;
23using aos::input::driver_station::JoystickAxis;
24using aos::input::driver_station::POVLocation;
25
Sabina Davisa8fed3d2020-02-22 21:44:57 -080026using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
27using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
28
29
Stephan Massaltd021f972020-01-05 20:41:23 -080030namespace y2020 {
31namespace input {
32namespace joysticks {
33
34namespace superstructure = y2020::control_loops::superstructure;
35
Sabina Davisa8fed3d2020-02-22 21:44:57 -080036// TODO(sabina): fix button locations.
37
38const ButtonLocation kShootFast(4, 1);
39const ButtonLocation kShootSlow(4, 2);
40const ButtonLocation kIntakeExtend(4, 3);
41const ButtonLocation kIntakeIn(4, 4);
42const ButtonLocation kSpit(4, 5);
43
Stephan Massaltd021f972020-01-05 20:41:23 -080044class Reader : public ::aos::input::ActionJoystickInput {
45 public:
46 Reader(::aos::EventLoop *event_loop)
47 : ::aos::input::ActionJoystickInput(
48 event_loop,
49 ::y2020::control_loops::drivetrain::GetDrivetrainConfig(),
50 ::aos::input::DrivetrainInputReader::InputType::kPistol, {}),
51 superstructure_goal_sender_(
52 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
53 superstructure_status_fetcher_(
54 event_loop->MakeFetcher<superstructure::Status>(
55 "/superstructure")) {}
56
57 void AutoEnded() override {
58 AOS_LOG(INFO, "Auto ended, assuming disc and have piece\n");
59 }
60
Sabina Davisa8fed3d2020-02-22 21:44:57 -080061 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Stephan Massaltd021f972020-01-05 20:41:23 -080062 superstructure_status_fetcher_.Fetch();
63 if (!superstructure_status_fetcher_.get()) {
64 AOS_LOG(ERROR, "Got no superstructure status message.\n");
65 return;
66 }
Sabina Davisa8fed3d2020-02-22 21:44:57 -080067
68 double hood_pos = constants::Values::kHoodRange().upper;
69 double intake_pos = constants::Values::kIntakeRange().lower;
70 double turret_pos = 0.0;
71 float roller_speed = 0.0f;
72 double accelerator_speed = 0.0;
73 double finisher_speed = 0.0;
74
75 if (data.IsPressed(kShootFast)) {
76 accelerator_speed = 300.0;
77 finisher_speed = 300.0;
78 }
79
80 else if (data.IsPressed(kShootSlow)) {
81 accelerator_speed = 30.0;
82 finisher_speed = 30.0;
83 }
84
85 if (data.IsPressed(kIntakeExtend)) {
86 intake_pos = constants::Values::kIntakeRange().middle();
87 }
88
89 if (data.IsPressed(kIntakeIn)) {
90 roller_speed = 6.0f;
91 } else if (data.IsPressed(kSpit)) {
92 roller_speed = -6.0f;
93 }
94
95 auto builder = superstructure_goal_sender_.MakeBuilder();
96
97 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
98 {
99 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
100 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
101 *builder.fbb(), hood_pos);
102
103 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
104 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
105 *builder.fbb(), intake_pos);
106
107 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
108 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
109 *builder.fbb(), turret_pos);
110
111 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
112 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed, finisher_speed);
113
114 superstructure::Goal::Builder superstructure_goal_builder =
115 builder.MakeBuilder<superstructure::Goal>();
116
117 superstructure_goal_builder.add_hood(hood_offset);
118 superstructure_goal_builder.add_intake(intake_offset);
119 superstructure_goal_builder.add_turret(turret_offset);
120 superstructure_goal_builder.add_roller_voltage(roller_speed);
121 superstructure_goal_builder.add_shooter(shooter_offset);
122
123 if (!builder.Send(superstructure_goal_builder.Finish())) {
124 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
125 }
126 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800127 }
128
129 private:
130 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
131
132 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
133};
134
135} // namespace joysticks
136} // namespace input
137} // namespace y2020
138
139int main() {
James Kuszmaulad8a8082020-02-14 21:21:58 -0800140 ::aos::InitNRT();
Stephan Massaltd021f972020-01-05 20:41:23 -0800141
142 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
143 aos::configuration::ReadConfig("config.json");
144
145 ::aos::ShmEventLoop event_loop(&config.message());
146 ::y2020::input::joysticks::Reader reader(&event_loop);
147
148 event_loop.Run();
149
150 ::aos::Cleanup();
151}