blob: bf5a3cbe0a246941cd1ebf5ff29b19f5831e24e1 [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
Comran Morshed41ed7c22015-11-04 21:03:37 +000011#include "y2014_bot3/autonomous/auto.q.h"
12#include "y2014_bot3/control_loops/drivetrain/drivetrain.q.h"
13#include "y2014_bot3/control_loops/drivetrain/drivetrain.h"
14#include "y2014_bot3/control_loops/rollers/rollers.q.h"
Comran Morshede9b12922015-11-04 19:46:48 +000015
16using ::aos::time::Time;
Comran Morshed41ed7c22015-11-04 21:03:37 +000017using ::y2014_bot3::control_loops::drivetrain_queue;
18using ::y2014_bot3::control_loops::rollers_queue;
Comran Morshede9b12922015-11-04 19:46:48 +000019
Comran Morshed41ed7c22015-11-04 21:03:37 +000020namespace y2014_bot3 {
Comran Morshede9b12922015-11-04 19:46:48 +000021namespace autonomous {
22
23namespace time = ::aos::time;
24
25static double left_initial_position, right_initial_position;
26
27bool ShouldExitAuto() {
Comran Morshed41ed7c22015-11-04 21:03:37 +000028 ::y2014_bot3::autonomous::autonomous.FetchLatest();
29 bool ans = !::y2014_bot3::autonomous::autonomous->run_auto;
Comran Morshede9b12922015-11-04 19:46:48 +000030 if (ans) {
31 LOG(INFO, "Time to exit auto mode\n");
32 }
33 return ans;
34}
35
Comran Morshede9b12922015-11-04 19:46:48 +000036void ResetDrivetrain() {
37 LOG(INFO, "resetting the drivetrain\n");
Comran Morshed41ed7c22015-11-04 21:03:37 +000038 control_loops::drivetrain_queue.goal.MakeWithBuilder()
Comran Morshede9b12922015-11-04 19:46:48 +000039 .control_loop_driving(false)
Comran Morshede9b12922015-11-04 19:46:48 +000040 .steering(0.0)
41 .throttle(0.0)
42 .left_goal(left_initial_position)
43 .left_velocity_goal(0)
44 .right_goal(right_initial_position)
45 .right_velocity_goal(0)
46 .Send();
47}
48
Comran Morshede9b12922015-11-04 19:46:48 +000049void InitializeEncoders() {
Comran Morshed41ed7c22015-11-04 21:03:37 +000050 control_loops::drivetrain_queue.status.FetchAnother();
Comran Morshede9b12922015-11-04 19:46:48 +000051 left_initial_position =
Comran Morshed41ed7c22015-11-04 21:03:37 +000052 control_loops::drivetrain_queue.status->filtered_left_position;
Comran Morshede9b12922015-11-04 19:46:48 +000053 right_initial_position =
Comran Morshed41ed7c22015-11-04 21:03:37 +000054 control_loops::drivetrain_queue.status->filtered_right_position;
Comran Morshede9b12922015-11-04 19:46:48 +000055}
56
57void HandleAuto() {
Comran Morshed41ed7c22015-11-04 21:03:37 +000058 ::aos::time::Time start_time = ::aos::time::Time::Now();
59 LOG(INFO, "Starting auto mode at %f\n", start_time.ToSeconds());
Comran Morshede9b12922015-11-04 19:46:48 +000060
Comran Morshed41ed7c22015-11-04 21:03:37 +000061 // TODO(comran): Add various options for different autos down below.
Comran Morshede9b12922015-11-04 19:46:48 +000062 ResetDrivetrain();
63 InitializeEncoders();
Comran Morshede9b12922015-11-04 19:46:48 +000064
Comran Morshed41ed7c22015-11-04 21:03:37 +000065 LOG(INFO, "Driving\n");
66 control_loops::drivetrain_queue.goal.MakeWithBuilder()
67 .control_loop_driving(false)
68 .highgear(false)
69 .quickturn(false)
70 .steering(0.0)
71 .throttle(0.5)
72 .left_goal(left_initial_position)
73 .left_velocity_goal(0)
74 .right_goal(right_initial_position)
75 .right_velocity_goal(0)
76 .Send();
77 time::SleepFor(time::Time::InSeconds(2.0));
78
79 control_loops::drivetrain_queue.goal.MakeWithBuilder()
80 .control_loop_driving(false)
81 .highgear(false)
82 .quickturn(false)
83 .steering(0.0)
84 .throttle(0.0)
85 .left_goal(left_initial_position)
86 .left_velocity_goal(0)
87 .right_goal(right_initial_position)
88 .right_velocity_goal(0)
89 .Send();
Comran Morshede9b12922015-11-04 19:46:48 +000090}
91
92} // namespace autonomous
Comran Morshed41ed7c22015-11-04 21:03:37 +000093} // namespace y2014_bot3