blob: 5fbb1e3c24a90088ad8aa75698c44d4285a64997 [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 Schuh366f7ed2017-03-11 21:57:14 -080028const ProfileParameters kSlowDrive = {2.0, 2.0};
29const ProfileParameters kSlowTurn = {3.0, 3.0};
Philipp Schrader85fca552017-03-05 00:30:50 +000030
Tyler Chatowf31da682017-01-22 01:39:40 +000031} // namespace
32
Philipp Schrader996a2a22017-02-22 05:02:48 +000033AutonomousActor::AutonomousActor(
34 ::frc971::autonomous::AutonomousActionQueueGroup *s)
35 : frc971::autonomous::BaseAutonomousActor(
36 s, control_loops::drivetrain::GetDrivetrainConfig()) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000037
Philipp Schrader996a2a22017-02-22 05:02:48 +000038bool AutonomousActor::RunAction(
39 const ::frc971::autonomous::AutonomousActionParams &params) {
Tyler Chatowf31da682017-01-22 01:39:40 +000040 monotonic_clock::time_point start_time = monotonic_clock::now();
41 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
Austin Schuh366f7ed2017-03-11 21:57:14 -080042 InitializeEncoders();
43 ResetDrivetrain();
Tyler Chatowf31da682017-01-22 01:39:40 +000044
45 switch (params.mode) {
46 case 0:
Austin Schuh366f7ed2017-03-11 21:57:14 -080047 default:
48 while (true) {
49 constexpr auto kDelayTime = chrono::milliseconds(1);
50 // Test case autonomous mode.
51 // Drives forward 1.0 meters and then turns 180 degrees.
52 StartDrive(1.0, 0.0, kSlowDrive, kSlowTurn);
53 if (!WaitForDriveDone()) return true;
54
55 this_thread::sleep_for(kDelayTime);
56 if (ShouldCancel()) return true;
57
58 StartDrive(0.0, M_PI, kSlowDrive, kSlowTurn);
59 if (!WaitForDriveDone()) return true;
60
61 this_thread::sleep_for(kDelayTime);
62 if (ShouldCancel()) return true;
63
64 StartDrive(1.0, 0.0, kSlowDrive, kSlowTurn);
65 if (!WaitForDriveDone()) return true;
66
67 this_thread::sleep_for(kDelayTime);
68 if (ShouldCancel()) return true;
69
70 StartDrive(0.0, M_PI, kSlowDrive, kSlowTurn);
71 if (!WaitForDriveDone()) return true;
72
73 this_thread::sleep_for(kDelayTime);
74 if (ShouldCancel()) return true;
75 }
76
Tyler Chatowf31da682017-01-22 01:39:40 +000077 break;
78
Tyler Chatowf31da682017-01-22 01:39:40 +000079 }
80
81 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
82
83 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
84 ::std::chrono::milliseconds(5) / 2);
85
86 while (!ShouldCancel()) {
87 phased_loop.SleepUntilNext();
88 }
89 LOG(DEBUG, "Done running\n");
90
91 return true;
92}
93
Tyler Chatowf31da682017-01-22 01:39:40 +000094} // namespace actors
95} // namespace y2017