blob: 51ea775b28e07152a9c4690e6dc4ea5f177f3265 [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);
Austin Schuh76af51e2017-04-09 18:32:38 -070057 set_turret_goal(0.0);
58 // Values good for blue:
59 // TODO(austin): Drive these off the auto switch.
60 //set_hood_goal(0.355 + 0.01 + 0.005);
61 //set_shooter_velocity(355.0);
62 set_hood_goal(0.355 + 0.01 + 0.005 + 0.01 + 0.01);
63 set_shooter_velocity(351.0);
Austin Schuh624088a2017-03-22 22:36:16 -070064 SendSuperstructureGoal();
65 StartDrive(0.0, -M_PI / 4.0 * kDriveDirection, kSlowDrive, kSlowTurn);
66 if (!WaitForDriveNear(0.05, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -080067
Austin Schuh624088a2017-03-22 22:36:16 -070068 this_thread::sleep_for(chrono::milliseconds(100));
Austin Schuh366f7ed2017-03-11 21:57:14 -080069
Austin Schuh624088a2017-03-22 22:36:16 -070070 StartDrive(0.0, (M_PI / 4.0 + 0.20) * kDriveDirection, kSlowDrive,
71 kSmashTurn);
72 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -080073
Austin Schuh624088a2017-03-22 22:36:16 -070074 set_vision_track(true);
Austin Schuh76af51e2017-04-09 18:32:38 -070075
76 set_indexer_angular_velocity(-1.8 * M_PI);
Austin Schuh624088a2017-03-22 22:36:16 -070077 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -080078
Austin Schuh624088a2017-03-22 22:36:16 -070079 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -080080
Austin Schuh624088a2017-03-22 22:36:16 -070081 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
82 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -070083
Austin Schuh624088a2017-03-22 22:36:16 -070084 LOG(INFO, "Started shooting at %f\n",
85 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -080086
Austin Schuh624088a2017-03-22 22:36:16 -070087 this_thread::sleep_for(start_time + chrono::seconds(9) -
88 monotonic_clock::now());
89 StartDrive(0.0, (-0.05) * kDriveDirection, kSlowDrive, kSlowTurn);
90 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -080091
Austin Schuh624088a2017-03-22 22:36:16 -070092 set_intake_max_velocity(0.05);
93 set_intake_goal(0.08);
94 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +000095
Austin Schuh624088a2017-03-22 22:36:16 -070096 this_thread::sleep_for(start_time + chrono::seconds(15) -
97 monotonic_clock::now());
98 if (ShouldCancel()) return true;
99
100 set_shooter_velocity(0.0);
101 set_indexer_angular_velocity(0.0);
102 SendSuperstructureGoal();
103
104 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000105 }
106
107 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
108
109 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
110 ::std::chrono::milliseconds(5) / 2);
111
112 while (!ShouldCancel()) {
113 phased_loop.SleepUntilNext();
114 }
115 LOG(DEBUG, "Done running\n");
116
117 return true;
118}
119
Tyler Chatowf31da682017-01-22 01:39:40 +0000120} // namespace actors
121} // namespace y2017