blob: d647d392d42fd2ddd622dd177c607c0289857a31 [file] [log] [blame]
Daniel Petti3fe36542013-09-25 04:18:24 +00001#include "aos/atom_code/init.h"
2#include "aos/common/logging/logging.h"
Daniel Petti0663d302013-10-27 05:39:39 +00003#include "aos/common/util/wrapping_counter.h"
Daniel Petti3fe36542013-09-25 04:18:24 +00004
Daniel Petti1f448512013-10-19 19:35:55 +00005#include "bot3/control_loops/drivetrain/drivetrain.q.h"
Daniel Petti3fe36542013-09-25 04:18:24 +00006#include "frc971/queues/GyroAngle.q.h"
Daniel Petti0663d302013-10-27 05:39:39 +00007#include "frc971/input/usb_receiver.h"
Daniel Petti3fe36542013-09-25 04:18:24 +00008
9#ifndef M_PI
10#define M_PI 3.14159265358979323846
11#endif
12
Daniel Petti1f448512013-10-19 19:35:55 +000013using ::bot3::control_loops::drivetrain;
Daniel Petti3fe36542013-09-25 04:18:24 +000014using ::frc971::sensors::gyro;
Daniel Petti0663d302013-10-27 05:39:39 +000015using ::aos::util::WrappingCounter;
16using ::frc971::USBReceiver;
Daniel Petti3fe36542013-09-25 04:18:24 +000017
Daniel Petti1f448512013-10-19 19:35:55 +000018namespace bot3 {
Daniel Petti3fe36542013-09-25 04:18:24 +000019namespace {
20
Daniel Pettib046c532013-10-29 03:40:40 +000021//TODO (danielp): Figure out whether the bigger gear is on the
22// encoder or not.
Daniel Petti3fe36542013-09-25 04:18:24 +000023inline double drivetrain_translate(int32_t in) {
24 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
Daniel Pettib046c532013-10-29 03:40:40 +000025 (44.0 / 32.0 /*encoder gears*/) * // the encoders are on the wheels.
26 (3.5 /*wheel diameter*/ * 2.54 / 100 * M_PI);
Daniel Petti3fe36542013-09-25 04:18:24 +000027}
28
Daniel Pettib046c532013-10-29 03:40:40 +000029// TODO (danielp): This needs to be modified eventually.
Daniel Petti3fe36542013-09-25 04:18:24 +000030inline double shooter_translate(int32_t in) {
31 return static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) *
32 (15.0 / 34.0) /*gears*/ * (2 * M_PI);
33}
34
Daniel Petti3fe36542013-09-25 04:18:24 +000035} // namespace
36
Daniel Petti0663d302013-10-27 05:39:39 +000037class GyroSensorReceiver : public USBReceiver {
38 virtual void ProcessData() override {
39 if (data()->robot_id != 0) {
40 LOG(ERROR, "gyro board sent data for robot id %hhd!"
41 " dip switches are %x\n",
42 data()->robot_id, data()->base_status & 0xF);
43 return;
44 } else {
45 LOG(DEBUG, "processing a packet dip switches %x\n",
46 data()->base_status & 0xF);
Daniel Petti3fe36542013-09-25 04:18:24 +000047 }
48
49 gyro.MakeWithBuilder()
Daniel Petti0663d302013-10-27 05:39:39 +000050 .angle(data()->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
Daniel Petti3fe36542013-09-25 04:18:24 +000051 .Send();
52
Daniel Petti3fe36542013-09-25 04:18:24 +000053 drivetrain.position.MakeWithBuilder()
James Kuszmaulf601eaa2013-10-31 06:25:09 -070054 .right_encoder(drivetrain_translate(data()->main.wrist))
55 .left_encoder(drivetrain_translate(data()->main.shooter))
Daniel Petti3fe36542013-09-25 04:18:24 +000056 .Send();
James Kuszmaulf601eaa2013-10-31 06:25:09 -070057 LOG(DEBUG, "right: %d left: %d angle: %lld \n",
58 data()->main.wrist, data()->main.shooter, data()->gyro_angle);
Daniel Petti3fe36542013-09-25 04:18:24 +000059 }
60
Daniel Petti0663d302013-10-27 05:39:39 +000061 WrappingCounter top_rise_;
62 WrappingCounter top_fall_;
63 WrappingCounter bottom_rise_;
64 WrappingCounter bottom_fall_delay_;
65 WrappingCounter bottom_fall_;
Daniel Petti3fe36542013-09-25 04:18:24 +000066};
67
Daniel Petti1f448512013-10-19 19:35:55 +000068} // namespace bot3
Daniel Petti3fe36542013-09-25 04:18:24 +000069
70int main() {
71 ::aos::Init();
Daniel Petti0663d302013-10-27 05:39:39 +000072 ::bot3::GyroSensorReceiver receiver;
Daniel Petti3fe36542013-09-25 04:18:24 +000073 while (true) {
74 receiver.RunIteration();
75 }
76 ::aos::Cleanup();
77}