blob: 36cf3481f4461c12e673cdba2dce6c750169cdc8 [file] [log] [blame]
Tyler Chatow6107aba2017-01-22 01:39:40 +00001#include "y2017/constants.h"
2
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
18#ifndef M_PI
19#define M_PI 3.14159265358979323846
20#endif
21
22namespace y2017 {
23namespace constants {
24
25// ///// Mutual constants between robots. /////
26const int Values::kZeroingSampleSize;
27
28constexpr double Values::kDrivetrainEncoderRatio;
29
30namespace {
31const uint16_t kCompTeamNumber = 971;
32const uint16_t kPracticeTeamNumber = 9971;
33
34// ///// Dynamic constants. /////
35
36const Values *DoGetValuesForTeam(uint16_t team) {
37 switch (team) {
38 case 1: // for tests
39 return new Values{
40 5.0, // drivetrain max speed
Diana Vandenberg223703d2017-01-28 17:39:53 -080041 0.0, // down error
Tyler Chatow6107aba2017-01-22 01:39:40 +000042 };
43 break;
44
45 case kCompTeamNumber:
46 return new Values{
47 5.0, // drivetrain max speed
Diana Vandenberg223703d2017-01-28 17:39:53 -080048 0.0, // down error
Tyler Chatow6107aba2017-01-22 01:39:40 +000049 };
50 break;
51
52 case kPracticeTeamNumber:
53 return new Values{
54 5.0, // drivetrain max speed
Diana Vandenberg223703d2017-01-28 17:39:53 -080055 0.0, // down error
Tyler Chatow6107aba2017-01-22 01:39:40 +000056 };
57 break;
58
59 default:
60 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
61 }
62}
63
64const Values *DoGetValues() {
65 uint16_t team = ::aos::network::GetTeamNumber();
66 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
67 return DoGetValuesForTeam(team);
68}
69
70} // namespace
71
72const Values &GetValues() {
73 static ::aos::Once<const Values> once(DoGetValues);
74 return *once.Get();
75}
76
77const Values &GetValuesForTeam(uint16_t team_number) {
78 static ::aos::Mutex mutex;
79 ::aos::MutexLocker locker(&mutex);
80
81 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
82 // race conditions.
83 static ::std::map<uint16_t, const Values *> values;
84
85 if (values.count(team_number) == 0) {
86 values[team_number] = DoGetValuesForTeam(team_number);
87#if __has_feature(address_sanitizer)
88 __lsan_ignore_object(values[team_number]);
89#endif
90 }
91 return *values[team_number];
92}
93
94} // namespace constants
95} // namespace y2017