blob: 6b63fabb6a9fab5a6972c5591cbf3565dd3c52df [file] [log] [blame]
Comran Morshede9b12922015-11-04 19:46:48 +00001#include <stdio.h>
2
Austin Schuhf2a50ba2016-12-24 16:16:26 -08003#include <chrono>
Comran Morshede9b12922015-11-04 19:46:48 +00004#include <memory>
5
Comran Morshede9b12922015-11-04 19:46:48 +00006#include "aos/common/logging/logging.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +00007#include "aos/common/logging/queue_logging.h"
Austin Schuhf2a50ba2016-12-24 16:16:26 -08008#include "aos/common/time.h"
9#include "aos/common/util/phased_loop.h"
10#include "aos/common/util/trapezoid_profile.h"
Adam Snaider83eae562016-09-10 16:47:33 -070011#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000012#include "y2014_bot3/autonomous/auto.q.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000013#include "y2014_bot3/control_loops/rollers/rollers.q.h"
Comran Morshede9b12922015-11-04 19:46:48 +000014
Adam Snaider83eae562016-09-10 16:47:33 -070015using ::frc971::control_loops::drivetrain_queue;
Comran Morshed41ed7c22015-11-04 21:03:37 +000016using ::y2014_bot3::control_loops::rollers_queue;
Comran Morshede9b12922015-11-04 19:46:48 +000017
Comran Morshed41ed7c22015-11-04 21:03:37 +000018namespace y2014_bot3 {
Comran Morshede9b12922015-11-04 19:46:48 +000019namespace autonomous {
20
Austin Schuhf2a50ba2016-12-24 16:16:26 -080021namespace chrono = ::std::chrono;
22using ::aos::monotonic_clock;
Comran Morshede9b12922015-11-04 19:46:48 +000023
24static double left_initial_position, right_initial_position;
25
26bool ShouldExitAuto() {
Comran Morshed41ed7c22015-11-04 21:03:37 +000027 ::y2014_bot3::autonomous::autonomous.FetchLatest();
28 bool ans = !::y2014_bot3::autonomous::autonomous->run_auto;
Comran Morshede9b12922015-11-04 19:46:48 +000029 if (ans) {
30 LOG(INFO, "Time to exit auto mode\n");
31 }
32 return ans;
33}
34
Comran Morshede9b12922015-11-04 19:46:48 +000035void ResetDrivetrain() {
36 LOG(INFO, "resetting the drivetrain\n");
Adam Snaider83eae562016-09-10 16:47:33 -070037 ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder()
Comran Morshede9b12922015-11-04 19:46:48 +000038 .control_loop_driving(false)
Austin Schuh2b1fce02018-03-02 20:05:20 -080039 .wheel(0.0)
Comran Morshede9b12922015-11-04 19:46:48 +000040 .throttle(0.0)
41 .left_goal(left_initial_position)
42 .left_velocity_goal(0)
43 .right_goal(right_initial_position)
44 .right_velocity_goal(0)
45 .Send();
46}
47
Comran Morshede9b12922015-11-04 19:46:48 +000048void InitializeEncoders() {
Adam Snaider83eae562016-09-10 16:47:33 -070049 ::frc971::control_loops::drivetrain_queue.status.FetchAnother();
Comran Morshede9b12922015-11-04 19:46:48 +000050 left_initial_position =
Adam Snaider83eae562016-09-10 16:47:33 -070051 ::frc971::control_loops::drivetrain_queue.status->estimated_left_position;
Comran Morshede9b12922015-11-04 19:46:48 +000052 right_initial_position =
Adam Snaider83eae562016-09-10 16:47:33 -070053 ::frc971::control_loops::drivetrain_queue.status->estimated_right_position;
Comran Morshede9b12922015-11-04 19:46:48 +000054}
55
56void HandleAuto() {
Austin Schuhf2a50ba2016-12-24 16:16:26 -080057 monotonic_clock::time_point start_time = monotonic_clock::now();
58 LOG(INFO, "Starting auto mode at %f\n",
59 chrono::duration_cast<chrono::duration<double>>(
60 start_time.time_since_epoch()).count());
Comran Morshede9b12922015-11-04 19:46:48 +000061
Comran Morshed41ed7c22015-11-04 21:03:37 +000062 // TODO(comran): Add various options for different autos down below.
Comran Morshede9b12922015-11-04 19:46:48 +000063 ResetDrivetrain();
64 InitializeEncoders();
Comran Morshede9b12922015-11-04 19:46:48 +000065
Comran Morshed41ed7c22015-11-04 21:03:37 +000066 LOG(INFO, "Driving\n");
Adam Snaider83eae562016-09-10 16:47:33 -070067 ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder()
Comran Morshed41ed7c22015-11-04 21:03:37 +000068 .control_loop_driving(false)
69 .highgear(false)
70 .quickturn(false)
Austin Schuh2b1fce02018-03-02 20:05:20 -080071 .wheel(0.0)
Comran Morshed41ed7c22015-11-04 21:03:37 +000072 .throttle(0.5)
73 .left_goal(left_initial_position)
74 .left_velocity_goal(0)
75 .right_goal(right_initial_position)
76 .right_velocity_goal(0)
77 .Send();
Austin Schuhf2a50ba2016-12-24 16:16:26 -080078 ::std::this_thread::sleep_for(chrono::seconds(2));
Comran Morshed41ed7c22015-11-04 21:03:37 +000079
Adam Snaider83eae562016-09-10 16:47:33 -070080 ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder()
Comran Morshed41ed7c22015-11-04 21:03:37 +000081 .control_loop_driving(false)
82 .highgear(false)
83 .quickturn(false)
Austin Schuh2b1fce02018-03-02 20:05:20 -080084 .wheel(0.0)
Comran Morshed41ed7c22015-11-04 21:03:37 +000085 .throttle(0.0)
86 .left_goal(left_initial_position)
87 .left_velocity_goal(0)
88 .right_goal(right_initial_position)
89 .right_velocity_goal(0)
90 .Send();
Comran Morshede9b12922015-11-04 19:46:48 +000091}
92
93} // namespace autonomous
Comran Morshed41ed7c22015-11-04 21:03:37 +000094} // namespace y2014_bot3