blob: 4e65dc4fea1aa6c828017ddca6e6a6996b4a78ea [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001#include <unistd.h>
2
3#include <cmath>
4#include <cstdio>
5#include <cstring>
6
7#include "aos/actions/actions.h"
8#include "aos/init.h"
9#include "aos/logging/logging.h"
10#include "aos/network/team_number.h"
11#include "aos/util/log_interval.h"
12#include "frc971/autonomous/base_autonomous_actor.h"
13#include "frc971/control_loops/drivetrain/localizer_generated.h"
14#include "frc971/control_loops/profiled_subsystem_generated.h"
15#include "frc971/input/action_joystick_input.h"
16#include "frc971/input/driver_station_data.h"
17#include "frc971/input/drivetrain_input.h"
18#include "frc971/input/joystick_input.h"
19#include "frc971/zeroing/wrap.h"
20#include "y2023/constants.h"
21#include "y2023/control_loops/drivetrain/drivetrain_base.h"
milind-udefab712023-02-20 22:22:02 -080022#include "y2023/control_loops/superstructure/arm/generated_graph.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080023#include "y2023/control_loops/superstructure/superstructure_goal_generated.h"
24#include "y2023/control_loops/superstructure/superstructure_status_generated.h"
25
26using frc971::CreateProfileParameters;
27using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
28using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
29using frc971::input::driver_station::ButtonLocation;
30using frc971::input::driver_station::ControlBit;
31using frc971::input::driver_station::JoystickAxis;
32using frc971::input::driver_station::POVLocation;
Maxwell Henderson5938a832023-02-23 09:33:15 -080033using y2023::control_loops::superstructure::RollerGoal;
Maxwell Hendersonad312342023-01-10 12:07:47 -080034
35namespace y2023 {
36namespace input {
37namespace joysticks {
38
milind-udefab712023-02-20 22:22:02 -080039// TODO(milind): add correct locations
Austin Schuh6dc925b2023-02-24 16:23:32 -080040const ButtonLocation kIntake(4, 5);
41const ButtonLocation kScore(4, 4);
milind-udefab712023-02-20 22:22:02 -080042const ButtonLocation kSpit(3, 3);
43
Austin Schuh6dc925b2023-02-24 16:23:32 -080044const ButtonLocation kWrist(4, 10);
45
Maxwell Hendersonad312342023-01-10 12:07:47 -080046namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080047namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080048
49class Reader : public ::frc971::input::ActionJoystickInput {
50 public:
51 Reader(::aos::EventLoop *event_loop)
52 : ::frc971::input::ActionJoystickInput(
53 event_loop,
54 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
55 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
56 superstructure_goal_sender_(
57 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
58 superstructure_status_fetcher_(
59 event_loop->MakeFetcher<superstructure::Status>(
60 "/superstructure")) {}
61
62 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
63
64 void HandleTeleop(
65 const ::frc971::input::driver_station::Data &data) override {
66 superstructure_status_fetcher_.Fetch();
67 if (!superstructure_status_fetcher_.get()) {
68 AOS_LOG(ERROR, "Got no superstructure status message.\n");
69 return;
70 }
71
Maxwell Henderson5938a832023-02-23 09:33:15 -080072 RollerGoal roller_goal = RollerGoal::IDLE;
milind-udefab712023-02-20 22:22:02 -080073
74 // TODO(milind): add more actions and paths
75 if (data.IsPressed(kIntake)) {
Maxwell Henderson5938a832023-02-23 09:33:15 -080076 roller_goal = RollerGoal::INTAKE;
77 arm_goal_position_ = arm::ScorePosIndex();
milind-udefab712023-02-20 22:22:02 -080078 } else if (data.IsPressed(kSpit)) {
Maxwell Henderson5938a832023-02-23 09:33:15 -080079 roller_goal = RollerGoal::SPIT;
80 arm_goal_position_ = arm::ScorePosIndex();
milind-udefab712023-02-20 22:22:02 -080081 } else if (data.IsPressed(kScore)) {
milind-u18a901d2023-02-17 21:51:55 -080082 arm_goal_position_ = arm::ScorePosIndex();
Austin Schuh6dc925b2023-02-24 16:23:32 -080083 } else {
84 arm_goal_position_ = arm::NeutralPosIndex();
85 }
86
87 double wrist_goal = 0.1;
88
89 if (data.IsPressed(kWrist)) {
90 wrist_goal = 1.5;
91 } else {
92 wrist_goal = 0.1;
milind-udefab712023-02-20 22:22:02 -080093 }
94
Maxwell Hendersonad312342023-01-10 12:07:47 -080095 {
96 auto builder = superstructure_goal_sender_.MakeBuilder();
97
Austin Schuh6dc925b2023-02-24 16:23:32 -080098 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
99 wrist_offset =
100 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
101 *builder.fbb(), wrist_goal,
102 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
103
Maxwell Hendersonad312342023-01-10 12:07:47 -0800104 superstructure::Goal::Builder superstructure_goal_builder =
105 builder.MakeBuilder<superstructure::Goal>();
milind-udefab712023-02-20 22:22:02 -0800106 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800107 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800108 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800109 if (builder.Send(superstructure_goal_builder.Finish()) !=
110 aos::RawSender::Error::kOk) {
111 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
112 }
113 }
114 }
115
116 private:
117 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
118
119 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800120
121 uint32_t arm_goal_position_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800122};
123
124} // namespace joysticks
125} // namespace input
126} // namespace y2023
127
128int main(int argc, char **argv) {
129 ::aos::InitGoogle(&argc, &argv);
130
131 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
132 aos::configuration::ReadConfig("aos_config.json");
133
134 ::aos::ShmEventLoop event_loop(&config.message());
135 ::y2023::input::joysticks::Reader reader(&event_loop);
136
137 event_loop.Run();
138
139 return 0;
140}