blob: 7910859a495ed61aa74bb4d43b6a6a8d54a6cc8b [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
Austin Schuhf0a637c2020-02-25 23:44:12 -080026using frc971::CreateProfileParameters;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080027using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
28using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
29
30
Stephan Massaltd021f972020-01-05 20:41:23 -080031namespace y2020 {
32namespace input {
33namespace joysticks {
34
35namespace superstructure = y2020::control_loops::superstructure;
36
Sabina Davisa8fed3d2020-02-22 21:44:57 -080037// TODO(sabina): fix button locations.
38
Austin Schuhf0a637c2020-02-25 23:44:12 -080039const ButtonLocation kShootFast(3, 16);
40const ButtonLocation kTurret(3, 15);
41const ButtonLocation kHood(3, 3);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080042const ButtonLocation kShootSlow(4, 2);
Austin Schuhf0a637c2020-02-25 23:44:12 -080043const ButtonLocation kIntakeExtend(3, 9);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080044const ButtonLocation kIntakeIn(4, 4);
45const ButtonLocation kSpit(4, 5);
46
Stephan Massaltd021f972020-01-05 20:41:23 -080047class Reader : public ::aos::input::ActionJoystickInput {
48 public:
49 Reader(::aos::EventLoop *event_loop)
50 : ::aos::input::ActionJoystickInput(
51 event_loop,
52 ::y2020::control_loops::drivetrain::GetDrivetrainConfig(),
53 ::aos::input::DrivetrainInputReader::InputType::kPistol, {}),
54 superstructure_goal_sender_(
55 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
56 superstructure_status_fetcher_(
57 event_loop->MakeFetcher<superstructure::Status>(
58 "/superstructure")) {}
59
60 void AutoEnded() override {
61 AOS_LOG(INFO, "Auto ended, assuming disc and have piece\n");
62 }
63
Sabina Davisa8fed3d2020-02-22 21:44:57 -080064 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Stephan Massaltd021f972020-01-05 20:41:23 -080065 superstructure_status_fetcher_.Fetch();
66 if (!superstructure_status_fetcher_.get()) {
67 AOS_LOG(ERROR, "Got no superstructure status message.\n");
68 return;
69 }
Sabina Davisa8fed3d2020-02-22 21:44:57 -080070
Austin Schuhf0a637c2020-02-25 23:44:12 -080071 double hood_pos = constants::Values::kHoodRange().middle();
72 double intake_pos = -0.89;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080073 double turret_pos = 0.0;
74 float roller_speed = 0.0f;
75 double accelerator_speed = 0.0;
76 double finisher_speed = 0.0;
77
Austin Schuhf0a637c2020-02-25 23:44:12 -080078 if (data.IsPressed(kTurret)) {
79 turret_pos = 3.5;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080080 }
81
Austin Schuhf0a637c2020-02-25 23:44:12 -080082 if (data.IsPressed(kHood)) {
83 hood_pos = 0.05;
84 }
85
86 if (data.IsPressed(kShootFast)) {
87 accelerator_speed = 600.0;
88 finisher_speed = 200.0;
89 } else if (data.IsPressed(kShootSlow)) {
Sabina Davisa8fed3d2020-02-22 21:44:57 -080090 accelerator_speed = 30.0;
91 finisher_speed = 30.0;
92 }
93
94 if (data.IsPressed(kIntakeExtend)) {
Austin Schuhf0a637c2020-02-25 23:44:12 -080095 intake_pos = 1.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080096 }
97
98 if (data.IsPressed(kIntakeIn)) {
99 roller_speed = 6.0f;
100 } else if (data.IsPressed(kSpit)) {
101 roller_speed = -6.0f;
102 }
103
104 auto builder = superstructure_goal_sender_.MakeBuilder();
105
106 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
107 {
108 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
109 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800110 *builder.fbb(), hood_pos,
111 CreateProfileParameters(*builder.fbb(), 0.5, 1.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800112
113 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
114 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800115 *builder.fbb(), intake_pos,
116 CreateProfileParameters(*builder.fbb(), 10.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800117
118 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
119 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800120 *builder.fbb(), turret_pos,
121 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800122
123 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800124 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
125 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800126
127 superstructure::Goal::Builder superstructure_goal_builder =
128 builder.MakeBuilder<superstructure::Goal>();
129
130 superstructure_goal_builder.add_hood(hood_offset);
131 superstructure_goal_builder.add_intake(intake_offset);
132 superstructure_goal_builder.add_turret(turret_offset);
133 superstructure_goal_builder.add_roller_voltage(roller_speed);
134 superstructure_goal_builder.add_shooter(shooter_offset);
135
136 if (!builder.Send(superstructure_goal_builder.Finish())) {
137 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
138 }
139 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800140 }
141
142 private:
143 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
144
145 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
146};
147
148} // namespace joysticks
149} // namespace input
150} // namespace y2020
151
152int main() {
James Kuszmaulad8a8082020-02-14 21:21:58 -0800153 ::aos::InitNRT();
Stephan Massaltd021f972020-01-05 20:41:23 -0800154
155 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
156 aos::configuration::ReadConfig("config.json");
157
158 ::aos::ShmEventLoop event_loop(&config.message());
159 ::y2020::input::joysticks::Reader reader(&event_loop);
160
161 event_loop.Run();
162
163 ::aos::Cleanup();
164}