blob: 625df573179b2b86a6c4780c16093cf34b56989e [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()
Daniel Petti0663d302013-10-27 05:39:39 +000054 .right_encoder(drivetrain_translate(data()->main.right_drive))
55 .left_encoder(-drivetrain_translate(data()->main.left_drive))
Daniel Petti3fe36542013-09-25 04:18:24 +000056 .Send();
Daniel Petti3fe36542013-09-25 04:18:24 +000057 }
58
Daniel Petti0663d302013-10-27 05:39:39 +000059 WrappingCounter top_rise_;
60 WrappingCounter top_fall_;
61 WrappingCounter bottom_rise_;
62 WrappingCounter bottom_fall_delay_;
63 WrappingCounter bottom_fall_;
Daniel Petti3fe36542013-09-25 04:18:24 +000064};
65
Daniel Petti1f448512013-10-19 19:35:55 +000066} // namespace bot3
Daniel Petti3fe36542013-09-25 04:18:24 +000067
68int main() {
69 ::aos::Init();
Daniel Petti0663d302013-10-27 05:39:39 +000070 ::bot3::GyroSensorReceiver receiver;
Daniel Petti3fe36542013-09-25 04:18:24 +000071 while (true) {
72 receiver.RunIteration();
73 }
74 ::aos::Cleanup();
75}