blob: 701f64860bc5b6d474c8edd99f6d66209b5a16ab [file] [log] [blame]
Tyler Chatowf31da682017-01-22 01:39:40 +00001#include "y2017/actors/autonomous_actor.h"
2
3#include <inttypes.h>
4
5#include <chrono>
6#include <cmath>
7
8#include "aos/common/util/phased_loop.h"
9#include "aos/common/logging/logging.h"
10
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Philipp Schrader996a2a22017-02-22 05:02:48 +000012#include "y2017/control_loops/drivetrain/drivetrain_base.h"
Tyler Chatowf31da682017-01-22 01:39:40 +000013
14namespace y2017 {
15namespace actors {
16using ::frc971::control_loops::drivetrain_queue;
17using ::aos::monotonic_clock;
18namespace chrono = ::std::chrono;
19namespace this_thread = ::std::this_thread;
20
21namespace {
Philipp Schrader85fca552017-03-05 00:30:50 +000022
Tyler Chatowf31da682017-01-22 01:39:40 +000023double DoubleSeconds(monotonic_clock::duration duration) {
24 return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration)
25 .count();
26}
Philipp Schrader85fca552017-03-05 00:30:50 +000027
Austin Schuh624088a2017-03-22 22:36:16 -070028const ProfileParameters kSlowDrive = {3.0, 2.0};
Austin Schuh366f7ed2017-03-11 21:57:14 -080029const ProfileParameters kSlowTurn = {3.0, 3.0};
Austin Schuh624088a2017-03-22 22:36:16 -070030const ProfileParameters kSmashTurn = {3.0, 5.0};
Philipp Schrader85fca552017-03-05 00:30:50 +000031
Tyler Chatowf31da682017-01-22 01:39:40 +000032} // namespace
33
Philipp Schrader996a2a22017-02-22 05:02:48 +000034AutonomousActor::AutonomousActor(
35 ::frc971::autonomous::AutonomousActionQueueGroup *s)
36 : frc971::autonomous::BaseAutonomousActor(
37 s, control_loops::drivetrain::GetDrivetrainConfig()) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000038
Philipp Schrader996a2a22017-02-22 05:02:48 +000039bool AutonomousActor::RunAction(
40 const ::frc971::autonomous::AutonomousActionParams &params) {
Tyler Chatowf31da682017-01-22 01:39:40 +000041 monotonic_clock::time_point start_time = monotonic_clock::now();
42 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
Austin Schuh624088a2017-03-22 22:36:16 -070043 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000044
45 switch (params.mode) {
46 case 0:
Austin Schuh624088a2017-03-22 22:36:16 -070047 default: {
48 constexpr double kDriveDirection = 1.0;
49 // Test case autonomous mode.
50 // Drives forward 1.0 meters and then turns 180 degrees.
51 set_intake_goal(0.07);
52 SendSuperstructureGoal();
53 StartDrive(-3.7, 0.0, kSlowDrive, kSlowTurn);
54 if (!WaitForDriveNear(2.75, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -080055
Austin Schuh624088a2017-03-22 22:36:16 -070056 set_intake_goal(0.23);
57 SendSuperstructureGoal();
58 StartDrive(0.0, -M_PI / 4.0 * kDriveDirection, kSlowDrive, kSlowTurn);
59 if (!WaitForDriveNear(0.05, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -080060
Austin Schuh624088a2017-03-22 22:36:16 -070061 this_thread::sleep_for(chrono::milliseconds(100));
Austin Schuh366f7ed2017-03-11 21:57:14 -080062
Austin Schuh624088a2017-03-22 22:36:16 -070063 StartDrive(0.0, (M_PI / 4.0 + 0.20) * kDriveDirection, kSlowDrive,
64 kSmashTurn);
65 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -080066
Austin Schuh624088a2017-03-22 22:36:16 -070067 set_vision_track(true);
68 set_turret_goal(0.0);
69 set_hood_goal(0.63);
70 set_shooter_velocity(371.0);
71 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -080072
Austin Schuh624088a2017-03-22 22:36:16 -070073 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -080074
Austin Schuh624088a2017-03-22 22:36:16 -070075 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
76 if (!WaitForDriveNear(0.05, 0.02)) return true;
77 set_indexer_angular_velocity(-1.8 * M_PI);
78 SendSuperstructureGoal();
79 LOG(INFO, "Started shooting at %f\n",
80 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -080081
Austin Schuh624088a2017-03-22 22:36:16 -070082 this_thread::sleep_for(start_time + chrono::seconds(9) -
83 monotonic_clock::now());
84 StartDrive(0.0, (-0.05) * kDriveDirection, kSlowDrive, kSlowTurn);
85 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -080086
Austin Schuh624088a2017-03-22 22:36:16 -070087 set_intake_max_velocity(0.05);
88 set_intake_goal(0.08);
89 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +000090
Austin Schuh624088a2017-03-22 22:36:16 -070091 this_thread::sleep_for(start_time + chrono::seconds(15) -
92 monotonic_clock::now());
93 if (ShouldCancel()) return true;
94
95 set_shooter_velocity(0.0);
96 set_indexer_angular_velocity(0.0);
97 SendSuperstructureGoal();
98
99 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000100 }
101
102 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
103
104 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
105 ::std::chrono::milliseconds(5) / 2);
106
107 while (!ShouldCancel()) {
108 phased_loop.SleepUntilNext();
109 }
110 LOG(DEBUG, "Done running\n");
111
112 return true;
113}
114
Tyler Chatowf31da682017-01-22 01:39:40 +0000115} // namespace actors
116} // namespace y2017