blob: 813b1ec3f68f4e0157ae5a92c4e21ab7cc2a94a7 [file] [log] [blame]
Comran Morshed0d6cf9b2015-06-17 19:29:57 +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"
9#include "aos/common/logging/queue_logging.h"
10
11#include "bot3/autonomous/auto.q.h"
12#include "bot3/control_loops/drivetrain/drivetrain.q.h"
Comran Morshed34f891d2015-09-15 22:04:43 +000013#include "bot3/control_loops/drivetrain/drivetrain.h"
Austin Schuhbd01a582015-09-21 00:05:31 +000014#include "bot3/control_loops/elevator/elevator.q.h"
15#include "bot3/control_loops/intake/intake.q.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000016
17using ::aos::time::Time;
18using ::bot3::control_loops::drivetrain_queue;
Austin Schuhbd01a582015-09-21 00:05:31 +000019using ::bot3::control_loops::intake_queue;
20using ::bot3::control_loops::elevator_queue;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000021
22namespace bot3 {
23namespace autonomous {
24
25struct ProfileParams {
26 double velocity;
27 double acceleration;
28};
29
30namespace time = ::aos::time;
31
Comran Morshed34f891d2015-09-15 22:04:43 +000032static double left_initial_position, right_initial_position;
33
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000034bool ShouldExitAuto() {
35 ::bot3::autonomous::autonomous.FetchLatest();
36 bool ans = !::bot3::autonomous::autonomous->run_auto;
37 if (ans) {
38 LOG(INFO, "Time to exit auto mode\n");
39 }
40 return ans;
41}
42
Comran Morshed34f891d2015-09-15 22:04:43 +000043void ResetDrivetrain() {
44 LOG(INFO, "resetting the drivetrain\n");
45 control_loops::drivetrain_queue.goal.MakeWithBuilder()
46 .control_loop_driving(false)
47 .steering(0.0)
48 .throttle(0.0)
49 .left_goal(left_initial_position)
50 .left_velocity_goal(0)
51 .right_goal(right_initial_position)
52 .right_velocity_goal(0)
53 .Send();
54}
55
56void InitializeEncoders() {
57 control_loops::drivetrain_queue.status.FetchAnother();
58 left_initial_position =
59 control_loops::drivetrain_queue.status->filtered_left_position;
60 right_initial_position =
61 control_loops::drivetrain_queue.status->filtered_right_position;
62}
63
64void GrabberForTime(double voltage, double wait_time) {
65 ::aos::time::Time now = ::aos::time::Time::Now();
66 ::aos::time::Time end_time = now + time::Time::InSeconds(wait_time);
67 LOG(INFO, "Starting to grab at %f for %f seconds\n", voltage, wait_time);
68
69 while (true) {
70 autonomous::can_grabber_control.MakeWithBuilder()
Austin Schuhbd01a582015-09-21 00:05:31 +000071 .can_grabber_voltage(voltage).can_grabbers(false).Send();
Comran Morshed34f891d2015-09-15 22:04:43 +000072
73 // Poll the running bit and auto done bits.
74 if (ShouldExitAuto()) {
75 return;
76 }
77 if (::aos::time::Time::Now() > end_time) {
78 LOG(INFO, "Done grabbing\n");
79 return;
80 }
81 ::aos::time::PhasedLoopXMS(5, 2500);
82 }
83}
84
85// Auto methods.
86void CanGrabberAuto() {
87 ResetDrivetrain();
88
89 // Launch can grabbers.
Austin Schuhbd01a582015-09-21 00:05:31 +000090 //GrabberForTime(12.0, 0.26);
91 GrabberForTime(6.0, 0.40);
Comran Morshed34f891d2015-09-15 22:04:43 +000092 if (ShouldExitAuto()) return;
93 InitializeEncoders();
94 ResetDrivetrain();
95 if (ShouldExitAuto()) return;
Austin Schuhbd01a582015-09-21 00:05:31 +000096 // Send our intake goals.
97 if (!intake_queue.goal.MakeWithBuilder().movement(0.0).claw_closed(true)
98 .Send()) {
99 LOG(ERROR, "Sending intake goal failed.\n");
100 }
101 {
102 auto new_elevator_goal = elevator_queue.goal.MakeMessage();
103 new_elevator_goal->max_velocity = 0.0;
104 new_elevator_goal->max_acceleration = 0.0;
105 new_elevator_goal->height = 0.030;
106 new_elevator_goal->velocity = 0.0;
107 new_elevator_goal->passive_support = true;
108 new_elevator_goal->can_support = false;
109
110 if (new_elevator_goal.Send()) {
111 LOG(DEBUG, "sending goals: elevator: %f\n", 0.03);
112 } else {
113 LOG(ERROR, "Sending elevator goal failed.\n");
114 }
115 }
116
117 const double kMoveBackDistance = 1.75;
118 //const double kMoveBackDistance = 0.0;
Comran Morshed34f891d2015-09-15 22:04:43 +0000119
120 // Drive backwards, and pulse the can grabbers again to tip the cans.
121 control_loops::drivetrain_queue.goal.MakeWithBuilder()
122 .control_loop_driving(true)
123 .steering(0.0)
124 .throttle(0.0)
Austin Schuhbd01a582015-09-21 00:05:31 +0000125 .left_goal(left_initial_position + kMoveBackDistance)
Comran Morshed34f891d2015-09-15 22:04:43 +0000126 .left_velocity_goal(0)
Austin Schuhbd01a582015-09-21 00:05:31 +0000127 .right_goal(right_initial_position + kMoveBackDistance)
Comran Morshed34f891d2015-09-15 22:04:43 +0000128 .right_velocity_goal(0)
129 .Send();
130 GrabberForTime(12.0, 0.02);
131 if (ShouldExitAuto()) return;
132
133 // We shouldn't need as much power at this point, so lower the can grabber
134 // voltages to avoid damaging the motors due to stalling.
135 GrabberForTime(4.0, 0.75);
136 if (ShouldExitAuto()) return;
137 GrabberForTime(-3.0, 0.25);
138 if (ShouldExitAuto()) return;
139 GrabberForTime(-12.0, 1.0);
140 if (ShouldExitAuto()) return;
141 GrabberForTime(-3.0, 12.0);
142}
143
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000144void HandleAuto() {
145 ::aos::time::Time start_time = ::aos::time::Time::Now();
146 LOG(INFO, "Starting auto mode at %f\n", start_time.ToSeconds());
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000147
Comran Morshed34f891d2015-09-15 22:04:43 +0000148 // TODO(comran): Add various options for different autos down below.
149 CanGrabberAuto();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000150}
151
152} // namespace autonomous
153} // namespace bot3