blob: 2049e8357f3f0f7c100a37f6b3115ffb5e96c3da [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"
Sabina Davisa8fed3d2020-02-22 21:44:57 -080016#include "y2020/constants.h"
Austin Schuhd58b2902020-03-01 19:28:04 -080017#include "y2020/control_loops/drivetrain/drivetrain_base.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"
Austin Schuhd58b2902020-03-01 19:28:04 -080020#include "y2020/setpoint_generated.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080021
22using aos::input::driver_station::ButtonLocation;
23using aos::input::driver_station::ControlBit;
24using aos::input::driver_station::JoystickAxis;
25using aos::input::driver_station::POVLocation;
26
Austin Schuhf0a637c2020-02-25 23:44:12 -080027using frc971::CreateProfileParameters;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080028using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
29using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
30
31
Stephan Massaltd021f972020-01-05 20:41:23 -080032namespace y2020 {
33namespace input {
34namespace joysticks {
35
36namespace superstructure = y2020::control_loops::superstructure;
37
Sabina Davisa8fed3d2020-02-22 21:44:57 -080038// TODO(sabina): fix button locations.
39
Austin Schuhf0a637c2020-02-25 23:44:12 -080040const ButtonLocation kShootFast(3, 16);
41const ButtonLocation kTurret(3, 15);
42const ButtonLocation kHood(3, 3);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080043const ButtonLocation kShootSlow(4, 2);
Austin Schuh43a220f2020-02-26 22:02:34 -080044const ButtonLocation kFeed(4, 1);
Austin Schuhf0a637c2020-02-25 23:44:12 -080045const ButtonLocation kIntakeExtend(3, 9);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080046const ButtonLocation kIntakeIn(4, 4);
Tyler Chatow1039e432020-02-28 21:37:50 -080047const ButtonLocation kSpit(4, 3);
48
49const ButtonLocation kWinch(3, 14);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080050
Stephan Massaltd021f972020-01-05 20:41:23 -080051class Reader : public ::aos::input::ActionJoystickInput {
52 public:
53 Reader(::aos::EventLoop *event_loop)
54 : ::aos::input::ActionJoystickInput(
55 event_loop,
56 ::y2020::control_loops::drivetrain::GetDrivetrainConfig(),
57 ::aos::input::DrivetrainInputReader::InputType::kPistol, {}),
58 superstructure_goal_sender_(
59 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
60 superstructure_status_fetcher_(
Austin Schuhd58b2902020-03-01 19:28:04 -080061 event_loop->MakeFetcher<superstructure::Status>("/superstructure")),
62 setpoint_fetcher_(event_loop->MakeFetcher<y2020::joysticks::Setpoint>(
63 "/superstructure")) {}
Stephan Massaltd021f972020-01-05 20:41:23 -080064
65 void AutoEnded() override {
66 AOS_LOG(INFO, "Auto ended, assuming disc and have piece\n");
67 }
68
Sabina Davisa8fed3d2020-02-22 21:44:57 -080069 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Stephan Massaltd021f972020-01-05 20:41:23 -080070 superstructure_status_fetcher_.Fetch();
71 if (!superstructure_status_fetcher_.get()) {
72 AOS_LOG(ERROR, "Got no superstructure status message.\n");
73 return;
74 }
Sabina Davisa8fed3d2020-02-22 21:44:57 -080075
Austin Schuhd58b2902020-03-01 19:28:04 -080076 setpoint_fetcher_.Fetch();
77
Austin Schuhf0a637c2020-02-25 23:44:12 -080078 double hood_pos = constants::Values::kHoodRange().middle();
79 double intake_pos = -0.89;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080080 double turret_pos = 0.0;
81 float roller_speed = 0.0f;
82 double accelerator_speed = 0.0;
83 double finisher_speed = 0.0;
Tyler Chatow1039e432020-02-28 21:37:50 -080084 double climber_speed = 0.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080085
Austin Schuhf0a637c2020-02-25 23:44:12 -080086 if (data.IsPressed(kTurret)) {
87 turret_pos = 3.5;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080088 }
89
Austin Schuhf0a637c2020-02-25 23:44:12 -080090 if (data.IsPressed(kHood)) {
Austin Schuhd58b2902020-03-01 19:28:04 -080091 hood_pos = 0.45;
92 } else {
93 if (setpoint_fetcher_.get()) {
94 hood_pos = setpoint_fetcher_->hood();
95 } else {
96 hood_pos = 0.58;
97 }
Austin Schuhf0a637c2020-02-25 23:44:12 -080098 }
99
100 if (data.IsPressed(kShootFast)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800101 if (setpoint_fetcher_.get()) {
102 accelerator_speed = setpoint_fetcher_->accelerator();
103 finisher_speed = setpoint_fetcher_->finisher();
104 } else {
105 accelerator_speed = 250.0;
106 finisher_speed = 500.0;
107 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800108 } else if (data.IsPressed(kShootSlow)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800109 accelerator_speed = 180.0;
110 finisher_speed = 375.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800111 }
112
113 if (data.IsPressed(kIntakeExtend)) {
Austin Schuh13e55522020-02-29 23:11:17 -0800114 intake_pos = 1.2;
115 roller_speed = 9.0f;
116 }
117
118 if (superstructure_status_fetcher_.get() &&
119 superstructure_status_fetcher_->intake()->position() > -0.5) {
120 roller_speed = std::max(roller_speed, 6.0f);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800121 }
122
Austin Schuh43a220f2020-02-26 22:02:34 -0800123 if (data.IsPressed(kFeed)) {
124 }
125
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800126 if (data.IsPressed(kIntakeIn)) {
127 roller_speed = 6.0f;
128 } else if (data.IsPressed(kSpit)) {
129 roller_speed = -6.0f;
130 }
131
Tyler Chatow1039e432020-02-28 21:37:50 -0800132 if (data.IsPressed(kWinch)) {
133 climber_speed = 12.0f;
134 }
135
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800136 auto builder = superstructure_goal_sender_.MakeBuilder();
137
138 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
139 {
140 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
141 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800142 *builder.fbb(), hood_pos,
Austin Schuh13e55522020-02-29 23:11:17 -0800143 CreateProfileParameters(*builder.fbb(), 0.7, 3.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800144
145 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
146 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800147 *builder.fbb(), intake_pos,
148 CreateProfileParameters(*builder.fbb(), 10.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800149
150 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
151 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800152 *builder.fbb(), turret_pos,
153 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800154
155 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800156 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
157 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800158
159 superstructure::Goal::Builder superstructure_goal_builder =
160 builder.MakeBuilder<superstructure::Goal>();
161
162 superstructure_goal_builder.add_hood(hood_offset);
163 superstructure_goal_builder.add_intake(intake_offset);
164 superstructure_goal_builder.add_turret(turret_offset);
165 superstructure_goal_builder.add_roller_voltage(roller_speed);
166 superstructure_goal_builder.add_shooter(shooter_offset);
Austin Schuh43a220f2020-02-26 22:02:34 -0800167 superstructure_goal_builder.add_shooting(data.IsPressed(kFeed));
Tyler Chatow1039e432020-02-28 21:37:50 -0800168 superstructure_goal_builder.add_climber_voltage(climber_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800169
170 if (!builder.Send(superstructure_goal_builder.Finish())) {
171 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
172 }
173 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800174 }
175
176 private:
177 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
178
179 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
Austin Schuhd58b2902020-03-01 19:28:04 -0800180
181 ::aos::Fetcher<y2020::joysticks::Setpoint> setpoint_fetcher_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800182};
183
184} // namespace joysticks
185} // namespace input
186} // namespace y2020
187
188int main() {
James Kuszmaulad8a8082020-02-14 21:21:58 -0800189 ::aos::InitNRT();
Stephan Massaltd021f972020-01-05 20:41:23 -0800190
191 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
192 aos::configuration::ReadConfig("config.json");
193
194 ::aos::ShmEventLoop event_loop(&config.message());
195 ::y2020::input::joysticks::Reader reader(&event_loop);
196
197 event_loop.Run();
198
199 ::aos::Cleanup();
200}