blob: 9dd93d68f24bd1afbc02fa828e418b6afc88e357 [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
28const ProfileParameters kSlowDrive = {0.8, 2.5};
29const ProfileParameters kSlowTurn = {0.8, 3.0};
30
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);
42
43 switch (params.mode) {
44 case 0:
Philipp Schrader85fca552017-03-05 00:30:50 +000045 // Test case autonomous mode.
46 // Drives forward 1.0 meters and then turns 180 degrees.
47 StartDrive(1.1, 0.0, kSlowDrive, kSlowTurn);
48 if (!WaitForDriveNear(1.0, 0.0)) return true;
49 StartDrive(0.0, M_PI / 2, kSlowDrive, kSlowTurn);
50 if (!WaitForDriveDone()) return true;
Tyler Chatowf31da682017-01-22 01:39:40 +000051 break;
52
53 default:
54 LOG(ERROR, "Invalid auto mode %d\n", params.mode);
55 return true;
56 }
57
58 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
59
60 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
61 ::std::chrono::milliseconds(5) / 2);
62
63 while (!ShouldCancel()) {
64 phased_loop.SleepUntilNext();
65 }
66 LOG(DEBUG, "Done running\n");
67
68 return true;
69}
70
Tyler Chatowf31da682017-01-22 01:39:40 +000071} // namespace actors
72} // namespace y2017