blob: 80f2b092bd0e09436014d18588c653573826ba5e [file] [log] [blame]
Comran Morshede9b12922015-11-04 19:46:48 +00001#include <stdio.h>
2
3#include <memory>
4
5#include "aos/common/util/phased_loop.h"
6#include "aos/common/time.h"
7#include "aos/common/util/trapezoid_profile.h"
8#include "aos/common/logging/logging.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +00009#include "aos/common/logging/queue_logging.h"
Comran Morshede9b12922015-11-04 19:46:48 +000010
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
15using ::aos::time::Time;
Adam Snaider83eae562016-09-10 16:47:33 -070016using ::frc971::control_loops::drivetrain_queue;
Comran Morshed41ed7c22015-11-04 21:03:37 +000017using ::y2014_bot3::control_loops::rollers_queue;
Comran Morshede9b12922015-11-04 19:46:48 +000018
Comran Morshed41ed7c22015-11-04 21:03:37 +000019namespace y2014_bot3 {
Comran Morshede9b12922015-11-04 19:46:48 +000020namespace autonomous {
21
22namespace time = ::aos::time;
23
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)
Comran Morshede9b12922015-11-04 19:46:48 +000039 .steering(0.0)
40 .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() {
Comran Morshed41ed7c22015-11-04 21:03:37 +000057 ::aos::time::Time start_time = ::aos::time::Time::Now();
58 LOG(INFO, "Starting auto mode at %f\n", start_time.ToSeconds());
Comran Morshede9b12922015-11-04 19:46:48 +000059
Comran Morshed41ed7c22015-11-04 21:03:37 +000060 // TODO(comran): Add various options for different autos down below.
Comran Morshede9b12922015-11-04 19:46:48 +000061 ResetDrivetrain();
62 InitializeEncoders();
Comran Morshede9b12922015-11-04 19:46:48 +000063
Comran Morshed41ed7c22015-11-04 21:03:37 +000064 LOG(INFO, "Driving\n");
Adam Snaider83eae562016-09-10 16:47:33 -070065 ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder()
Comran Morshed41ed7c22015-11-04 21:03:37 +000066 .control_loop_driving(false)
67 .highgear(false)
68 .quickturn(false)
69 .steering(0.0)
70 .throttle(0.5)
71 .left_goal(left_initial_position)
72 .left_velocity_goal(0)
73 .right_goal(right_initial_position)
74 .right_velocity_goal(0)
75 .Send();
76 time::SleepFor(time::Time::InSeconds(2.0));
77
Adam Snaider83eae562016-09-10 16:47:33 -070078 ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder()
Comran Morshed41ed7c22015-11-04 21:03:37 +000079 .control_loop_driving(false)
80 .highgear(false)
81 .quickturn(false)
82 .steering(0.0)
83 .throttle(0.0)
84 .left_goal(left_initial_position)
85 .left_velocity_goal(0)
86 .right_goal(right_initial_position)
87 .right_velocity_goal(0)
88 .Send();
Comran Morshede9b12922015-11-04 19:46:48 +000089}
90
91} // namespace autonomous
Comran Morshed41ed7c22015-11-04 21:03:37 +000092} // namespace y2014_bot3