Apollo 10.0
自动驾驶开放平台
sim_control_util.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16#pragma once
17
18#include <math.h>
19#include <stdio.h>
20
21#include <iostream>
22#include <string>
23#include <vector>
24
25#include <google/protobuf/text_format.h>
26
27#include "cyber/common/log.h"
28#include "cyber/cyber.h"
29
30namespace apollo {
31namespace dreamview {
32
34 public:
35 double interpolate_1d(const double& p1, const double& p2,
36 const double& frac1);
37 double interpolated_find(const std::vector<double>& range_table,
38 const std::vector<double>& val_table,
39 double to_find);
40
41 static double sigmoid(const double value);
42 static double relu(const double value);
43 double normalize(const double value, const double mean, const double std);
44
45 template <class P>
46 bool load_binary_file(const std::string& filename, P* pb_out) {
47 std::fstream input(filename, std::ios::in | std::ios::binary);
48 return pb_out->ParseFromIstream(&input);
49 }
50
51 template <class P>
52 bool load_text_file(const std::string& filename, P* pb_out) {
53 std::fstream input(filename, std::ios::in);
54 std::string input_data((std::istreambuf_iterator<char>(input)),
55 std::istreambuf_iterator<char>());
56
57 if (input_data.empty()) {
58 return false;
59 }
60
61 return google::protobuf::TextFormat::ParseFromString(input_data, pb_out);
62 }
63
64 template <class P>
65 bool load_file_to_proto(const std::string& filename, P* pb_out) {
66 if (ends_with(filename, ".bin")) {
67 if (!load_binary_file(filename, pb_out) &&
68 !load_text_file(filename, pb_out)) {
69 return false;
70 }
71 } else {
72 if (!load_text_file(filename, pb_out) &&
73 !load_binary_file(filename, pb_out)) {
74 return false;
75 }
76 }
77
78 return true;
79 }
80
81 bool ends_with(const std::string& original, const std::string& pattern) {
82 return original.length() >= pattern.length() &&
83 original.substr(original.length() - pattern.length()) == pattern;
84 }
85
86 // delta function
87 int delta_function(double value, double threshold) {
88 return static_cast<int>(value > threshold);
89 }
90
91 // calculate \eta from rise time and peak time
92 double get_eta(double rise_time, double peak_time) {
93 if (peak_time <= 0.0) {
94 AFATAL << "peak_time should be positive";
95 }
96 return cos(M_PI - rise_time / (peak_time / M_PI));
97 }
98
99 // calculate \tau_s (1/omega); only suitable for under-damped system (eta < 1)
100 double get_tau_s(double peak_time, double eta) {
101 if (eta > 1.0) {
102 AFATAL << "not an underdamped system";
103 }
104 return peak_time / sqrt(1 - eta * eta);
105 }
106};
107
108} // namespace dreamview
109} // namespace apollo
int delta_function(double value, double threshold)
bool load_file_to_proto(const std::string &filename, P *pb_out)
double interpolate_1d(const double &p1, const double &p2, const double &frac1)
double interpolated_find(const std::vector< double > &range_table, const std::vector< double > &val_table, double to_find)
bool load_text_file(const std::string &filename, P *pb_out)
static double relu(const double value)
double normalize(const double value, const double mean, const double std)
static double sigmoid(const double value)
bool ends_with(const std::string &original, const std::string &pattern)
double get_eta(double rise_time, double peak_time)
double get_tau_s(double peak_time, double eta)
bool load_binary_file(const std::string &filename, P *pb_out)
#define AFATAL
Definition log.h:45
class register implement
Definition arena_queue.h:37
Definition future.h:29