blob: 452829893b44011067a69ab182b6c18e92195f93 [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;
milind upadhyayaec1aee2020-10-13 13:44:33 -070082 float roller_speed_compensation = 0.0f;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080083 double accelerator_speed = 0.0;
84 double finisher_speed = 0.0;
Tyler Chatow1039e432020-02-28 21:37:50 -080085 double climber_speed = 0.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080086
Austin Schuhf0a637c2020-02-25 23:44:12 -080087 if (data.IsPressed(kTurret)) {
88 turret_pos = 3.5;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080089 }
90
Austin Schuhf0a637c2020-02-25 23:44:12 -080091 if (data.IsPressed(kHood)) {
Austin Schuhd58b2902020-03-01 19:28:04 -080092 hood_pos = 0.45;
93 } else {
94 if (setpoint_fetcher_.get()) {
95 hood_pos = setpoint_fetcher_->hood();
96 } else {
97 hood_pos = 0.58;
98 }
Austin Schuhf0a637c2020-02-25 23:44:12 -080099 }
100
101 if (data.IsPressed(kShootFast)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800102 if (setpoint_fetcher_.get()) {
103 accelerator_speed = setpoint_fetcher_->accelerator();
104 finisher_speed = setpoint_fetcher_->finisher();
105 } else {
106 accelerator_speed = 250.0;
107 finisher_speed = 500.0;
108 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800109 } else if (data.IsPressed(kShootSlow)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800110 accelerator_speed = 180.0;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700111 finisher_speed = 300.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800112 }
113
114 if (data.IsPressed(kIntakeExtend)) {
Austin Schuh13e55522020-02-29 23:11:17 -0800115 intake_pos = 1.2;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700116 roller_speed = 7.0f;
117 roller_speed_compensation = 2.0f;
Austin Schuh13e55522020-02-29 23:11:17 -0800118 }
119
120 if (superstructure_status_fetcher_.get() &&
121 superstructure_status_fetcher_->intake()->position() > -0.5) {
122 roller_speed = std::max(roller_speed, 6.0f);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700123 roller_speed_compensation = 2.0f;
Austin Schuh43a220f2020-02-26 22:02:34 -0800124 }
125
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800126 if (data.IsPressed(kIntakeIn)) {
127 roller_speed = 6.0f;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700128 roller_speed_compensation = 2.0f;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800129 } else if (data.IsPressed(kSpit)) {
130 roller_speed = -6.0f;
131 }
132
Tyler Chatow1039e432020-02-28 21:37:50 -0800133 if (data.IsPressed(kWinch)) {
134 climber_speed = 12.0f;
135 }
136
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800137 auto builder = superstructure_goal_sender_.MakeBuilder();
138
139 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
140 {
141 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
142 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800143 *builder.fbb(), hood_pos,
Austin Schuh13e55522020-02-29 23:11:17 -0800144 CreateProfileParameters(*builder.fbb(), 0.7, 3.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800145
146 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
147 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800148 *builder.fbb(), intake_pos,
149 CreateProfileParameters(*builder.fbb(), 10.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800150
151 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
152 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800153 *builder.fbb(), turret_pos,
154 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800155
156 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800157 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
158 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800159
160 superstructure::Goal::Builder superstructure_goal_builder =
161 builder.MakeBuilder<superstructure::Goal>();
162
163 superstructure_goal_builder.add_hood(hood_offset);
164 superstructure_goal_builder.add_intake(intake_offset);
165 superstructure_goal_builder.add_turret(turret_offset);
166 superstructure_goal_builder.add_roller_voltage(roller_speed);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700167 superstructure_goal_builder.add_roller_speed_compensation(
168 roller_speed_compensation);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800169 superstructure_goal_builder.add_shooter(shooter_offset);
Austin Schuh43a220f2020-02-26 22:02:34 -0800170 superstructure_goal_builder.add_shooting(data.IsPressed(kFeed));
Tyler Chatow1039e432020-02-28 21:37:50 -0800171 superstructure_goal_builder.add_climber_voltage(climber_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800172
173 if (!builder.Send(superstructure_goal_builder.Finish())) {
174 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
175 }
176 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800177 }
178
179 private:
180 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
181
182 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
Austin Schuhd58b2902020-03-01 19:28:04 -0800183
184 ::aos::Fetcher<y2020::joysticks::Setpoint> setpoint_fetcher_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800185};
186
187} // namespace joysticks
188} // namespace input
189} // namespace y2020
190
Austin Schuh094d09b2020-11-20 23:26:52 -0800191int main(int argc, char **argv) {
192 ::aos::InitGoogle(&argc, &argv);
Stephan Massaltd021f972020-01-05 20:41:23 -0800193
194 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
195 aos::configuration::ReadConfig("config.json");
196
197 ::aos::ShmEventLoop event_loop(&config.message());
198 ::y2020::input::joysticks::Reader reader(&event_loop);
199
200 event_loop.Run();
201
Austin Schuhae87e312020-08-01 16:15:01 -0700202 return 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800203}