blob: 75a4b26cf3f7d731a277d73ef006b5b704b9decf [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 Schuh43a220f2020-02-26 22:02:34 -080043const ButtonLocation kFeed(4, 1);
Austin Schuhf0a637c2020-02-25 23:44:12 -080044const ButtonLocation kIntakeExtend(3, 9);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080045const ButtonLocation kIntakeIn(4, 4);
Tyler Chatow1039e432020-02-28 21:37:50 -080046const ButtonLocation kSpit(4, 3);
47
48const ButtonLocation kWinch(3, 14);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080049
Stephan Massaltd021f972020-01-05 20:41:23 -080050class Reader : public ::aos::input::ActionJoystickInput {
51 public:
52 Reader(::aos::EventLoop *event_loop)
53 : ::aos::input::ActionJoystickInput(
54 event_loop,
55 ::y2020::control_loops::drivetrain::GetDrivetrainConfig(),
56 ::aos::input::DrivetrainInputReader::InputType::kPistol, {}),
57 superstructure_goal_sender_(
58 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
59 superstructure_status_fetcher_(
60 event_loop->MakeFetcher<superstructure::Status>(
61 "/superstructure")) {}
62
63 void AutoEnded() override {
64 AOS_LOG(INFO, "Auto ended, assuming disc and have piece\n");
65 }
66
Sabina Davisa8fed3d2020-02-22 21:44:57 -080067 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Stephan Massaltd021f972020-01-05 20:41:23 -080068 superstructure_status_fetcher_.Fetch();
69 if (!superstructure_status_fetcher_.get()) {
70 AOS_LOG(ERROR, "Got no superstructure status message.\n");
71 return;
72 }
Sabina Davisa8fed3d2020-02-22 21:44:57 -080073
Austin Schuhf0a637c2020-02-25 23:44:12 -080074 double hood_pos = constants::Values::kHoodRange().middle();
75 double intake_pos = -0.89;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080076 double turret_pos = 0.0;
77 float roller_speed = 0.0f;
78 double accelerator_speed = 0.0;
79 double finisher_speed = 0.0;
Tyler Chatow1039e432020-02-28 21:37:50 -080080 double climber_speed = 0.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080081
Austin Schuhf0a637c2020-02-25 23:44:12 -080082 if (data.IsPressed(kTurret)) {
83 turret_pos = 3.5;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080084 }
85
Austin Schuhf0a637c2020-02-25 23:44:12 -080086 if (data.IsPressed(kHood)) {
87 hood_pos = 0.05;
88 }
89
90 if (data.IsPressed(kShootFast)) {
Austin Schuh43a220f2020-02-26 22:02:34 -080091 accelerator_speed = 500.0;
92 finisher_speed = 500.0;
Austin Schuhf0a637c2020-02-25 23:44:12 -080093 } else if (data.IsPressed(kShootSlow)) {
Sabina Davisa8fed3d2020-02-22 21:44:57 -080094 accelerator_speed = 30.0;
95 finisher_speed = 30.0;
96 }
97
98 if (data.IsPressed(kIntakeExtend)) {
Austin Schuhf0a637c2020-02-25 23:44:12 -080099 intake_pos = 1.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800100 }
101
Austin Schuh43a220f2020-02-26 22:02:34 -0800102 if (data.IsPressed(kFeed)) {
103 }
104
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800105 if (data.IsPressed(kIntakeIn)) {
106 roller_speed = 6.0f;
107 } else if (data.IsPressed(kSpit)) {
108 roller_speed = -6.0f;
109 }
110
Tyler Chatow1039e432020-02-28 21:37:50 -0800111 if (data.IsPressed(kWinch)) {
112 climber_speed = 12.0f;
113 }
114
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800115 auto builder = superstructure_goal_sender_.MakeBuilder();
116
117 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
118 {
119 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
120 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800121 *builder.fbb(), hood_pos,
122 CreateProfileParameters(*builder.fbb(), 0.5, 1.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800123
124 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
125 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800126 *builder.fbb(), intake_pos,
127 CreateProfileParameters(*builder.fbb(), 10.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800128
129 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
130 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800131 *builder.fbb(), turret_pos,
132 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800133
134 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800135 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
136 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800137
138 superstructure::Goal::Builder superstructure_goal_builder =
139 builder.MakeBuilder<superstructure::Goal>();
140
141 superstructure_goal_builder.add_hood(hood_offset);
142 superstructure_goal_builder.add_intake(intake_offset);
143 superstructure_goal_builder.add_turret(turret_offset);
144 superstructure_goal_builder.add_roller_voltage(roller_speed);
145 superstructure_goal_builder.add_shooter(shooter_offset);
Austin Schuh43a220f2020-02-26 22:02:34 -0800146 superstructure_goal_builder.add_shooting(data.IsPressed(kFeed));
Tyler Chatow1039e432020-02-28 21:37:50 -0800147 superstructure_goal_builder.add_climber_voltage(climber_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800148
149 if (!builder.Send(superstructure_goal_builder.Finish())) {
150 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
151 }
152 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800153 }
154
155 private:
156 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
157
158 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
159};
160
161} // namespace joysticks
162} // namespace input
163} // namespace y2020
164
165int main() {
James Kuszmaulad8a8082020-02-14 21:21:58 -0800166 ::aos::InitNRT();
Stephan Massaltd021f972020-01-05 20:41:23 -0800167
168 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
169 aos::configuration::ReadConfig("config.json");
170
171 ::aos::ShmEventLoop event_loop(&config.message());
172 ::y2020::input::joysticks::Reader reader(&event_loop);
173
174 event_loop.Run();
175
176 ::aos::Cleanup();
177}