Skip to content

Commit cfb2c71

Browse files
author
orin07
committed
default car.lua file
1 parent 2959ba1 commit cfb2c71

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

src/vesc_driver/vesc_driver.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include <cassert>
77
#include <cmath>
88
#include <sstream>
9+
#include <fstream>
10+
#include <unistd.h>
11+
#include <regex>
912

1013
#include "boost/bind.hpp"
1114
#include "gflags/gflags.h"
@@ -69,9 +72,37 @@ VescDriver::VescDriver(rclcpp::Node::SharedPtr nh,
6972
t_last_command_(0),
7073
t_last_joystick_(0) {
7174
{
72-
// Load config.
75+
// Load config. Ensure car.lua exists; if it doesn't, create it using
76+
// the hostname. Hostnames like "orin07" will produce car_name = "car07".
77+
std::string car_path = FLAGS_config_dir + "/car.lua";
78+
{
79+
std::ifstream car_in(car_path);
80+
if (!car_in.good()) {
81+
char hn[256] = {0};
82+
if (gethostname(hn, sizeof(hn)) != 0) {
83+
LOG(WARNING) << "Failed to get hostname; defaulting car number to 00";
84+
}
85+
std::string hs(hn);
86+
std::smatch m;
87+
std::regex r("([0-9]+)$");
88+
std::string digits = "00";
89+
if (std::regex_search(hs, m, r) && m.size() > 1) {
90+
digits = m[1];
91+
}
92+
std::string car_name = "car" + digits;
93+
std::ofstream car_out(car_path);
94+
if (car_out) {
95+
car_out << "car_name = \"" << car_name << "\";\n";
96+
car_out.close();
97+
LOG(INFO) << "Generated " << car_path << " with car_name " << car_name;
98+
} else {
99+
LOG(WARNING) << "Unable to create " << car_path << "; proceeding without it.";
100+
}
101+
}
102+
}
103+
73104
config_reader::ConfigReader reader({
74-
FLAGS_config_dir + "/car.lua",
105+
car_path,
75106
FLAGS_config_dir + "/vesc.lua",
76107
FLAGS_config_dir + "/joystick.lua"
77108
});

0 commit comments

Comments
 (0)