blob: 5b31505a05c4d261901a60cfdd24796d8290ba6b [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"
James Kuszmaulf7f5ec12013-11-01 17:58:58 -07006#include "bot3/control_loops/shooter/shooter_motor.q.h"
Daniel Petti3fe36542013-09-25 04:18:24 +00007#include "frc971/queues/GyroAngle.q.h"
Daniel Petti0663d302013-10-27 05:39:39 +00008#include "frc971/input/usb_receiver.h"
Daniel Petti3fe36542013-09-25 04:18:24 +00009
10#ifndef M_PI
11#define M_PI 3.14159265358979323846
12#endif
13
Daniel Petti1f448512013-10-19 19:35:55 +000014using ::bot3::control_loops::drivetrain;
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070015using ::bot3::control_loops::shooter;
Daniel Petti3fe36542013-09-25 04:18:24 +000016using ::frc971::sensors::gyro;
Daniel Petti0663d302013-10-27 05:39:39 +000017using ::aos::util::WrappingCounter;
18using ::frc971::USBReceiver;
Daniel Petti3fe36542013-09-25 04:18:24 +000019
Daniel Petti1f448512013-10-19 19:35:55 +000020namespace bot3 {
Daniel Petti3fe36542013-09-25 04:18:24 +000021namespace {
22
Daniel Pettib046c532013-10-29 03:40:40 +000023//TODO (danielp): Figure out whether the bigger gear is on the
24// encoder or not.
Daniel Petti3fe36542013-09-25 04:18:24 +000025inline double drivetrain_translate(int32_t in) {
26 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070027 (32.0 / 44.0 /*encoder gears*/) * // the encoders are on the wheels.
Daniel Pettib046c532013-10-29 03:40:40 +000028 (3.5 /*wheel diameter*/ * 2.54 / 100 * M_PI);
Daniel Petti3fe36542013-09-25 04:18:24 +000029}
30
Daniel Pettib046c532013-10-29 03:40:40 +000031// TODO (danielp): This needs to be modified eventually.
Daniel Petti3fe36542013-09-25 04:18:24 +000032inline double shooter_translate(int32_t in) {
33 return static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) *
34 (15.0 / 34.0) /*gears*/ * (2 * M_PI);
35}
36
Daniel Petti3fe36542013-09-25 04:18:24 +000037} // namespace
38
Daniel Petti0663d302013-10-27 05:39:39 +000039class GyroSensorReceiver : public USBReceiver {
40 virtual void ProcessData() override {
41 if (data()->robot_id != 0) {
42 LOG(ERROR, "gyro board sent data for robot id %hhd!"
43 " dip switches are %x\n",
44 data()->robot_id, data()->base_status & 0xF);
45 return;
46 } else {
47 LOG(DEBUG, "processing a packet dip switches %x\n",
48 data()->base_status & 0xF);
Daniel Petti3fe36542013-09-25 04:18:24 +000049 }
50
51 gyro.MakeWithBuilder()
Daniel Petti0663d302013-10-27 05:39:39 +000052 .angle(data()->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
Daniel Petti3fe36542013-09-25 04:18:24 +000053 .Send();
54
Daniel Petti3fe36542013-09-25 04:18:24 +000055 drivetrain.position.MakeWithBuilder()
James Kuszmaulf601eaa2013-10-31 06:25:09 -070056 .right_encoder(drivetrain_translate(data()->main.wrist))
57 .left_encoder(drivetrain_translate(data()->main.shooter))
Daniel Petti3fe36542013-09-25 04:18:24 +000058 .Send();
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070059 LOG(DEBUG, "right: %lf left: %lf angle: %lld \n",
60 drivetrain_translate(data()->main.wrist),
61 drivetrain_translate(data()->main.shooter), data()->gyro_angle);
62
63 shooter.position.MakeWithBuilder().position(drivetrain_translate(data()->main.shooter)).Send();
Daniel Petti3fe36542013-09-25 04:18:24 +000064 }
65
Daniel Petti0663d302013-10-27 05:39:39 +000066 WrappingCounter top_rise_;
67 WrappingCounter top_fall_;
68 WrappingCounter bottom_rise_;
69 WrappingCounter bottom_fall_delay_;
70 WrappingCounter bottom_fall_;
Daniel Petti3fe36542013-09-25 04:18:24 +000071};
72
Daniel Petti1f448512013-10-19 19:35:55 +000073} // namespace bot3
Daniel Petti3fe36542013-09-25 04:18:24 +000074
75int main() {
76 ::aos::Init();
Daniel Petti0663d302013-10-27 05:39:39 +000077 ::bot3::GyroSensorReceiver receiver;
Daniel Petti3fe36542013-09-25 04:18:24 +000078 while (true) {
79 receiver.RunIteration();
80 }
81 ::aos::Cleanup();
82}