blob: 52285f7e5c7c758063f23df1f4d786a3b8fbce85 [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 {
27namespace {
28
29const uint16_t kCompTeamNumber = 971;
30const uint16_t kPracticeTeamNumber = 9971;
Comran Morshed9a9948c2016-01-16 15:58:04 +000031
Comran Morshed6c6a0a92016-01-17 12:45:16 +000032// TODO(constants): Update these to what we're using this year.
Comran Morshed9a9948c2016-01-16 15:58:04 +000033const double kCompDrivetrainEncoderRatio =
34 (18.0 / 50.0) /*output reduction*/ * (56.0 / 30.0) /*encoder gears*/;
35const double kCompLowGearRatio = 18.0 / 60.0 * 18.0 / 50.0;
36const double kCompHighGearRatio = 28.0 / 50.0 * 18.0 / 50.0;
37
38const double kPracticeDrivetrainEncoderRatio = kCompDrivetrainEncoderRatio;
39const double kPracticeLowGearRatio = kCompLowGearRatio;
40const double kPracticeHighGearRatio = kCompHighGearRatio;
41
42const ShifterHallEffect kCompLeftDriveShifter{2.61, 2.33, 4.25, 3.28, 0.2, 0.7};
Comran Morshed6c6a0a92016-01-17 12:45:16 +000043const ShifterHallEffect kCompRightDriveShifter{2.94, 4.31, 4.32,
44 3.25, 0.2, 0.7};
Comran Morshed9a9948c2016-01-16 15:58:04 +000045
Comran Morshed6c6a0a92016-01-17 12:45:16 +000046const ShifterHallEffect kPracticeLeftDriveShifter{2.80, 3.05, 4.15,
47 3.2, 0.2, 0.7};
48const ShifterHallEffect kPracticeRightDriveShifter{2.90, 3.75, 3.80,
49 2.98, 0.2, 0.7};
Comran Morshed9a9948c2016-01-16 15:58:04 +000050
51const double kRobotWidth = 25.0 / 100.0 * 2.54;
52
Comran Morshed9a9948c2016-01-16 15:58:04 +000053const Values *DoGetValuesForTeam(uint16_t team) {
54 switch (team) {
55 case 1: // for tests
56 return new Values{
57 kCompDrivetrainEncoderRatio,
58 kCompLowGearRatio,
59 kCompHighGearRatio,
60 kCompLeftDriveShifter,
61 kCompRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +000062 0.5,
Comran Morshed6c6a0a92016-01-17 12:45:16 +000063 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
64 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
65 5.0, // drivetrain max speed
Comran Morshed9a9948c2016-01-16 15:58:04 +000066 };
67 break;
68 case kCompTeamNumber:
69 return new Values{
70 kCompDrivetrainEncoderRatio,
71 kCompLowGearRatio,
72 kCompHighGearRatio,
73 kCompLeftDriveShifter,
74 kCompRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +000075 kRobotWidth,
Comran Morshed6c6a0a92016-01-17 12:45:16 +000076 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
77 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
78 5.0, // drivetrain max speed
Comran Morshed9a9948c2016-01-16 15:58:04 +000079 };
80 break;
81 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +000082 return new Values{
83 kPracticeDrivetrainEncoderRatio,
84 kPracticeLowGearRatio,
85 kPracticeHighGearRatio,
86 kPracticeLeftDriveShifter,
87 kPracticeRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +000088 kRobotWidth,
Comran Morshed6c6a0a92016-01-17 12:45:16 +000089 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
90 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
91 5.0, // drivetrain max speed
Comran Morshed9a9948c2016-01-16 15:58:04 +000092 };
93 break;
94 default:
95 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
96 }
97}
98
99const Values *DoGetValues() {
100 uint16_t team = ::aos::network::GetTeamNumber();
101 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
102 return DoGetValuesForTeam(team);
103}
104
105} // namespace
106
107const Values &GetValues() {
108 static ::aos::Once<const Values> once(DoGetValues);
109 return *once.Get();
110}
111
112const Values &GetValuesForTeam(uint16_t team_number) {
113 static ::aos::Mutex mutex;
114 ::aos::MutexLocker locker(&mutex);
115
116 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
117 // race conditions.
118 static ::std::map<uint16_t, const Values *> values;
119
120 if (values.count(team_number) == 0) {
121 values[team_number] = DoGetValuesForTeam(team_number);
122#if __has_feature(address_sanitizer)
123 __lsan_ignore_object(values[team_number]);
124#endif
125 }
126 return *values[team_number];
127}
128
129} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000130} // namespace y2016