blob: 56fb2ada76a6766e6655cd5f93d4b978f67b8910 [file] [log] [blame]
Comran Morshed6c6a0a92016-01-17 12:45:16 +00001#include "y2016/constants.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +00002
3#include <math.h>
4#include <stdint.h>
5#include <inttypes.h>
6
7#include <map>
8
9#if __has_feature(address_sanitizer)
10#include "sanitizer/lsan_interface.h"
11#endif
12
13#include "aos/common/logging/logging.h"
14#include "aos/common/once.h"
15#include "aos/common/network/team_number.h"
16#include "aos/common/mutex.h"
17
Comran Morshed6c6a0a92016-01-17 12:45:16 +000018#include "y2016/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
19#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000020
21#ifndef M_PI
22#define M_PI 3.14159265358979323846
23#endif
24
Comran Morshed6c6a0a92016-01-17 12:45:16 +000025namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000026namespace constants {
Comran Morshed9a9948c2016-01-16 15:58:04 +000027
Comran Morshed225f0b92016-02-10 20:34:27 +000028// ///// Mutual constants between robots. /////
29const int Values::kZeroingSampleSize;
30
31constexpr double Values::kDrivetrainEncoderRatio, Values::kLowGearRatio,
32 Values::kHighGearRatio, Values::kTurnWidth, Values::kShooterEncoderRatio,
33 Values::kIntakeEncoderRatio, Values::kShoulderEncoderRatio,
34 Values::kWristEncoderRatio, Values::kIntakePotRatio,
35 Values::kShoulderPotRatio, Values::kWristPotRatio,
36 Values::kIntakeEncoderIndexDifference,
37 Values::kShoulderEncoderIndexDifference,
38 Values::kWristEncoderIndexDifference;
39constexpr Values::Range Values::kIntakeRange, Values::kShoulderRange,
40 Values::kWristRange;
41
42namespace {
Comran Morshed9a9948c2016-01-16 15:58:04 +000043const uint16_t kCompTeamNumber = 971;
44const uint16_t kPracticeTeamNumber = 9971;
Comran Morshed9a9948c2016-01-16 15:58:04 +000045
Comran Morshed225f0b92016-02-10 20:34:27 +000046// ///// Dynamic constants. /////
Austin Schuh04c894e2016-02-13 23:54:42 -080047
Comran Morshed9a9948c2016-01-16 15:58:04 +000048const Values *DoGetValuesForTeam(uint16_t team) {
49 switch (team) {
Comran Morshed225f0b92016-02-10 20:34:27 +000050 case 1:
Comran Morshed9a9948c2016-01-16 15:58:04 +000051 case kCompTeamNumber:
52 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +000053 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080054
55 // Intake
56 {
Comran Morshed225f0b92016-02-10 20:34:27 +000057 0.0,
58 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
59 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080060 },
61
62 // Shoulder
63 {
Comran Morshed225f0b92016-02-10 20:34:27 +000064 0.0,
65 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
66 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080067 },
68
69 // Wrist
70 {
Comran Morshed225f0b92016-02-10 20:34:27 +000071 0.0,
72 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
73 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080074 },
Comran Morshed9a9948c2016-01-16 15:58:04 +000075 };
76 break;
77 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +000078 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +000079 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080080
81 // Intake
82 {
Comran Morshed225f0b92016-02-10 20:34:27 +000083 0.0,
84 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
85 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080086 },
87
88 // Shoulder
89 {
Comran Morshed225f0b92016-02-10 20:34:27 +000090 0.0,
91 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
92 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080093 },
94
95 // Wrist
96 {
Comran Morshed225f0b92016-02-10 20:34:27 +000097 0.0,
98 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
99 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800100 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000101 };
102 break;
103 default:
104 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
105 }
106}
107
108const Values *DoGetValues() {
109 uint16_t team = ::aos::network::GetTeamNumber();
110 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
111 return DoGetValuesForTeam(team);
112}
113
114} // namespace
115
116const Values &GetValues() {
117 static ::aos::Once<const Values> once(DoGetValues);
118 return *once.Get();
119}
120
121const Values &GetValuesForTeam(uint16_t team_number) {
122 static ::aos::Mutex mutex;
123 ::aos::MutexLocker locker(&mutex);
124
125 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
126 // race conditions.
127 static ::std::map<uint16_t, const Values *> values;
128
129 if (values.count(team_number) == 0) {
130 values[team_number] = DoGetValuesForTeam(team_number);
131#if __has_feature(address_sanitizer)
132 __lsan_ignore_object(values[team_number]);
133#endif
134 }
135 return *values[team_number];
136}
137
138} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000139} // namespace y2016