pilot_apollo_bridge.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "modules/adc/pilot_apollo_bridge/proto/gpsimu.pb.h"
  2. #include "cyber/cyber.h"
  3. #include "cyber/time/rate.h"
  4. #include "cyber/time/time.h"
  5. #include "modules/common/adapters/adapter_gflags.h"
  6. #include "modules/common_msgs/localization_msgs/gps.pb.h"
  7. #include "modules/common_msgs/sensor_msgs/ins.pb.h"
  8. #include "modules/common_msgs/localization_msgs/imu.pb.h"
  9. //car数据定义的引用,可以看出其定义来源于一个proto
  10. // using apollo::communication::proto::Car;
  11. using ::apollo::localization::Gps;
  12. #include <iostream>
  13. #include <chrono>
  14. #include <qglobal.h>
  15. #include "Eigen/Geometry"
  16. #include "modulecomm.h"
  17. #define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
  18. #include <proj_api.h>
  19. projPJ wgs84pj_source_;
  20. projPJ utm_target_;
  21. const char *WGS84_TEXT = "+proj=latlong +ellps=WGS84";
  22. ::apollo::drivers::gnss::Ins ins_;
  23. ::apollo::drivers::gnss::InsStat ins_stat_;
  24. std::shared_ptr<apollo::cyber::Writer<apollo::localization::Gps>> gps_writer_ = nullptr;
  25. std::shared_ptr<apollo::cyber::Writer<apollo::drivers::gnss::InsStat>> insstat_writer_ = nullptr;
  26. std::shared_ptr<apollo::cyber::Writer<apollo::localization::CorrectedImu>> corrimu_writer_ = nullptr;
  27. //const double DEG_TO_RAD = M_PI / 180.0;
  28. constexpr double DEG_TO_RAD_LOCAL = M_PI / 180.0;
  29. constexpr float FLOAT_NAN = std::numeric_limits<float>::quiet_NaN();
  30. constexpr double azimuth_deg_to_yaw_rad(double azimuth) {
  31. return (90.0 - azimuth) * DEG_TO_RAD;
  32. }
  33. // File expires on: December 2017
  34. // There's two concept about 'time'.
  35. // 1. Unix time : It counts seconds since Jan 1,1970.
  36. // Unix time doesn't include the leap seconds.
  37. //
  38. // 2. GPS time : It counts seconds since Jan 6,1980.
  39. // GPS time does include the leap seconds.
  40. //
  41. // Leap seconds was added in every a few years, See details:
  42. // http://tycho.usno.navy.mil/leapsec.html
  43. // https://en.wikipedia.org/wiki/Leap_second
  44. //
  45. /* leap_seconds table since 1980.
  46. +======+========+========+======+========+========+
  47. | Year | Jun 30 | Dec 31 | Year | Jun 30 | Dec 31 |
  48. +======+========+========+======+========+========+
  49. | 1980 | (already +19) | 1994 | +1 | 0 |
  50. +------+--------+--------+------+--------+--------+
  51. | 1981 | +1 | 0 | 1995 | 0 | +1 |
  52. +------+--------+--------+------+--------+--------+
  53. | 1982 | +1 | 0 | 1997 | +1 | 0 |
  54. +------+--------+--------+------+--------+--------+
  55. | 1983 | +1 | 0 | 1998 | 0 | +1 |
  56. +------+--------+--------+------+--------+--------+
  57. | 1985 | +1 | 0 | 2005 | 0 | +1 |
  58. +------+--------+--------+------+--------+--------+
  59. | 1987 | 0 | +1 | 2008 | 0 | +1 |
  60. +------+--------+--------+------+--------+--------+
  61. | 1989 | 0 | +1 | 2012 | +1 | 0 |
  62. +------+--------+--------+------+--------+--------+
  63. | 1990 | 0 | +1 | 2015 | +1 | 0 |
  64. +------+--------+--------+------+--------+--------+
  65. | 1992 | +1 | 0 | 2016 | 0 | +1 |
  66. +------+--------+--------+------+--------+--------+
  67. | 1993 | +1 | 0 | 2017 | 0 | 0 |
  68. +------+--------+--------+------+--------+--------+
  69. Current TAI - UTC = 37. (mean that: 2017 - 1970/01/01 = 37 seconds)
  70. */
  71. // We build a lookup table to describe relationship that between UTC and
  72. // Leap_seconds.
  73. //
  74. // Column1: UTC time diff (second).
  75. // Shell Example:
  76. // date +%s -d"Jan 1, 2017 00:00:00"
  77. // return 1483257600.
  78. //
  79. // date +%s -d"Jan 1, 1970 00:00:00"
  80. // return 28800.
  81. //
  82. // The diff between 1970/01/01 and 2017/01/01 is 1483228800.
  83. // (1483257600-28800)
  84. //
  85. // Column2: Leap seconds diff with GPS basetime(Jan 6,1980).
  86. // We Know that The leap_seconds have been already added 37 seconds
  87. // util now(2017).
  88. // So we can calculate leap_seconds diff between GPS (from Jan 6,1980)
  89. // and UTC time.
  90. // calc with the formula.
  91. static const int32_t LEAP_SECONDS[][2] = {
  92. {1483228800, 18}, // 2017/01/01
  93. {1435708800, 17}, // 2015/07/01
  94. {1341100800, 16}, // 2012/07/01
  95. {1230768000, 15}, // 2009/01/01
  96. {1136073600, 14}, // 2006/01/01
  97. {915148800, 13}, // 1999/01/01
  98. {867711600, 12}, // 1997/07/01
  99. {820480320, 11}, // 1996/01/01 ;)
  100. //....
  101. //..
  102. // etc.
  103. };
  104. // seconds that UNIX time afront of GPS, without leap seconds.
  105. // Shell:
  106. // time1 = date +%s -d"Jan 6, 1980 00:00:00"
  107. // time2 = date +%s -d"Jan 1, 1970 00:00:00"
  108. // dif_tick = time1-time2
  109. // 315964800 = 315993600 - 28800
  110. const int32_t GPS_AND_SYSTEM_DIFF_SECONDS = 315964800;
  111. template <class T, size_t N>
  112. constexpr size_t array_size(T (&)[N]) {
  113. return N;
  114. }
  115. template <typename T>
  116. T unix2gps(const T unix_seconds) {
  117. for (size_t i = 0; i < array_size(LEAP_SECONDS); ++i) {
  118. if (unix_seconds >= LEAP_SECONDS[i][0]) {
  119. return unix_seconds - (GPS_AND_SYSTEM_DIFF_SECONDS - LEAP_SECONDS[i][1]);
  120. }
  121. }
  122. return static_cast<T>(0);
  123. }
  124. template <typename T>
  125. T gps2unix(const T gps_seconds) {
  126. for (size_t i = 0; i < array_size(LEAP_SECONDS); ++i) {
  127. T result = gps_seconds + (GPS_AND_SYSTEM_DIFF_SECONDS - LEAP_SECONDS[i][1]);
  128. if (result >= LEAP_SECONDS[i][0]) {
  129. return result;
  130. }
  131. }
  132. return static_cast<T>(0);
  133. }
  134. enum class InsStatus : uint32_t {
  135. INACTIVE = 0,
  136. ALIGNING,
  137. HIGH_VARIANCE,
  138. SOLUTION_GOOD,
  139. SOLUTION_FREE = 6,
  140. ALIGNMENT_COMPLETE,
  141. DETERMINING_ORIENTATION,
  142. WAITING_INITIAL_POS,
  143. NONE = std::numeric_limits<uint32_t>::max(),
  144. };
  145. void ListenGPSIMU(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  146. {
  147. (void)index;
  148. (void)dt;
  149. (void)strmemname;
  150. iv::gps::gpsimu xgpsimu;
  151. if(!xgpsimu.ParseFromArray(strdata,nSize))
  152. {
  153. std::cout<<"ListenRaw Parse error."<<std::endl;
  154. return;
  155. }
  156. int64_t timenow = std::chrono::system_clock::now().time_since_epoch().count();
  157. double seconds = timenow * 1e-9;//gps_week * SECONDS_PER_WEEK + gps_millisecs * 1e-3;
  158. ins_.mutable_position()->set_lon(xgpsimu.lon());
  159. ins_.mutable_position()->set_lat(xgpsimu.lat());
  160. ins_.mutable_position()->set_height(xgpsimu.height());
  161. ins_.mutable_euler_angles()->set_x(xgpsimu.roll() * DEG_TO_RAD);
  162. ins_.mutable_euler_angles()->set_y(-xgpsimu.pitch() * DEG_TO_RAD);
  163. ins_.mutable_euler_angles()->set_z(azimuth_deg_to_yaw_rad(xgpsimu.heading()));
  164. ins_.mutable_linear_velocity()->set_x(xgpsimu.ve());
  165. ins_.mutable_linear_velocity()->set_y(xgpsimu.vn());
  166. ins_.mutable_linear_velocity()->set_z(xgpsimu.vd());
  167. ins_.set_type(apollo::drivers::gnss::Ins::GOOD);
  168. // ins_.set_measurement_time(seconds);
  169. ins_.set_measurement_time(unix2gps(seconds));
  170. ins_.mutable_header()->set_timestamp_sec(apollo::cyber::Time::Now().ToSecond());
  171. double unix_sec = seconds;//apollo::drivers::util::gps2unix(seconds);
  172. ins_stat_.mutable_header()->set_timestamp_sec(unix_sec);
  173. ins_stat_.set_ins_status(3);
  174. ins_stat_.set_pos_type(xgpsimu.rtk_state());
  175. ::apollo::drivers::gnss::Ins *ins = &ins_;
  176. auto imu = std::make_shared<apollo::localization::CorrectedImu>();
  177. // double unix_sec = seconds;//apollo::drivers::util::gps2unix(ins->measurement_time());
  178. imu->mutable_header()->set_timestamp_sec(unix_sec);
  179. auto *imu_msg = imu->mutable_imu();
  180. imu_msg->mutable_linear_acceleration()->set_x(
  181. -ins->linear_acceleration().y());
  182. imu_msg->mutable_linear_acceleration()->set_y(ins->linear_acceleration().x());
  183. imu_msg->mutable_linear_acceleration()->set_z(ins->linear_acceleration().z());
  184. imu_msg->mutable_angular_velocity()->set_x(-ins->angular_velocity().y());
  185. imu_msg->mutable_angular_velocity()->set_y(ins->angular_velocity().x());
  186. imu_msg->mutable_angular_velocity()->set_z(ins->angular_velocity().z());
  187. imu_msg->mutable_euler_angles()->set_x(ins->euler_angles().x());
  188. imu_msg->mutable_euler_angles()->set_y(-ins->euler_angles().y());
  189. imu_msg->mutable_euler_angles()->set_z(ins->euler_angles().z() -
  190. 90 * DEG_TO_RAD_LOCAL);
  191. corrimu_writer_->Write(imu);
  192. auto gps = std::make_shared<apollo::localization::Gps>();
  193. // double unix_sec = apollo::drivers::util::gps2unix(ins->measurement_time());
  194. gps->mutable_header()->set_timestamp_sec(unix_sec);
  195. auto *gps_msg = gps->mutable_localization();
  196. // 1. pose xyz
  197. double x = ins->position().lon();
  198. double y = ins->position().lat();
  199. x *= DEG_TO_RAD_LOCAL;
  200. y *= DEG_TO_RAD_LOCAL;
  201. pj_transform(wgs84pj_source_, utm_target_, 1, 1, &x, &y, NULL);
  202. gps_msg->mutable_position()->set_x(x);
  203. gps_msg->mutable_position()->set_y(y);
  204. gps_msg->mutable_position()->set_z(ins->position().height());
  205. // 2. orientation
  206. Eigen::Quaterniond q =
  207. Eigen::AngleAxisd(ins->euler_angles().z() - 90 * DEG_TO_RAD_LOCAL,
  208. Eigen::Vector3d::UnitZ()) *
  209. Eigen::AngleAxisd(-ins->euler_angles().y(), Eigen::Vector3d::UnitX()) *
  210. Eigen::AngleAxisd(ins->euler_angles().x(), Eigen::Vector3d::UnitY());
  211. gps_msg->mutable_orientation()->set_qx(q.x());
  212. gps_msg->mutable_orientation()->set_qy(q.y());
  213. gps_msg->mutable_orientation()->set_qz(q.z());
  214. gps_msg->mutable_orientation()->set_qw(q.w());
  215. gps_msg->mutable_linear_velocity()->set_x(ins->linear_velocity().x());
  216. gps_msg->mutable_linear_velocity()->set_y(ins->linear_velocity().y());
  217. gps_msg->mutable_linear_velocity()->set_z(ins->linear_velocity().z());
  218. gps_writer_->Write(gps);
  219. auto ins_stat = std::make_shared<apollo::drivers::gnss::InsStat>();
  220. // common::util::FillHeader("gnss", ins_stat);
  221. ins_stat->mutable_header()->set_timestamp_sec(unix_sec);
  222. ins_stat->set_ins_status(3);
  223. ins_stat->set_pos_type(xgpsimu.rtk_state());
  224. insstat_writer_->Write(ins_stat);
  225. }
  226. void InitIns(){
  227. ins_.mutable_position_covariance()->Resize(9, FLOAT_NAN);
  228. ins_.mutable_euler_angles_covariance()->Resize(9, FLOAT_NAN);
  229. ins_.mutable_linear_velocity_covariance()->Resize(9, FLOAT_NAN);
  230. }
  231. int main(int argc, char *argv[]) {
  232. // 初始化一个cyber框架
  233. apollo::cyber::Init(argv[0]);
  234. // 创建talker节点
  235. auto talker_node = apollo::cyber::CreateNode("pilot_apollo_bridge");
  236. InitIns();
  237. wgs84pj_source_ = pj_init_plus(WGS84_TEXT);
  238. utm_target_ = pj_init_plus("+proj=utm +zone=10 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs");
  239. gps_writer_ = talker_node->CreateWriter<Gps>(FLAGS_gps_topic);
  240. insstat_writer_ = talker_node->CreateWriter<apollo::drivers::gnss::InsStat>(FLAGS_ins_stat_topic);
  241. corrimu_writer_ = talker_node->CreateWriter<apollo::localization::CorrectedImu>(FLAGS_imu_topic);
  242. // 从节点创建一个Topic,来实现对车速的查看
  243. // auto talker = talker_node->CreateWriter<Car>("car_speed");
  244. // AINFO << "I'll start telling you the current speed of the car.";
  245. void * paraw = iv::modulecomm::RegisterRecv("hcp2_gpsimu",ListenGPSIMU);
  246. (void)paraw;
  247. std::cout<<" enter talker."<<std::endl;
  248. //设置初始速度为0,然后速度每秒增加5km/h
  249. uint64_t speed = 0;
  250. while (apollo::cyber::OK()) {
  251. // auto msg = std::make_shared<Car>();
  252. // msg->set_speed(speed);
  253. //假设车速持续增加
  254. speed += 5;
  255. // talker->Write(msg);
  256. sleep(1);
  257. }
  258. return 0;
  259. }