mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-27 18:27:05 +08:00
i2c_launcher: Dehardcode battery index
This commit is contained in:
committed by
Ramon Roche
parent
f067d7a6d8
commit
65c180852f
@@ -40,11 +40,17 @@
|
|||||||
|
|
||||||
constexpr I2CLauncher::I2CDevice I2CLauncher::_devices[];
|
constexpr I2CLauncher::I2CDevice I2CLauncher::_devices[];
|
||||||
|
|
||||||
I2CLauncher::I2CLauncher(int bus) :
|
I2CLauncher::I2CLauncher(int bus, int batt_index) :
|
||||||
ModuleParams(nullptr),
|
ModuleParams(nullptr),
|
||||||
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(bus)),
|
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(bus)),
|
||||||
_bus(bus)
|
_bus(bus)
|
||||||
{
|
{
|
||||||
|
if (_batt_index == -1) {
|
||||||
|
_batt_index = bus;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
_batt_index = batt_index;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
I2CLauncher::~I2CLauncher()
|
I2CLauncher::~I2CLauncher()
|
||||||
@@ -87,10 +93,10 @@ void I2CLauncher::Run()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
scan_i2c_bus(_bus);
|
scan_i2c_bus(_bus, _batt_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2CLauncher::scan_i2c_bus(int bus)
|
void I2CLauncher::scan_i2c_bus(int bus, int batt_index)
|
||||||
{
|
{
|
||||||
struct i2c_master_s *i2c_dev = px4_i2cbus_initialize(bus);
|
struct i2c_master_s *i2c_dev = px4_i2cbus_initialize(bus);
|
||||||
|
|
||||||
@@ -163,7 +169,7 @@ void I2CLauncher::scan_i2c_bus(int bus)
|
|||||||
|
|
||||||
if (found) {
|
if (found) {
|
||||||
char buf[32];
|
char buf[32];
|
||||||
snprintf(buf, sizeof(buf), "%s -X -b %d -t %d start", _devices[i].cmd, bus, bus);
|
snprintf(buf, sizeof(buf), "%s -X -b %d -t %d start", _devices[i].cmd, bus, batt_index);
|
||||||
|
|
||||||
PX4_INFO("Found address 0x%x, running '%s'\n", _devices[i].i2c_addr, buf);
|
PX4_INFO("Found address 0x%x, running '%s'\n", _devices[i].i2c_addr, buf);
|
||||||
|
|
||||||
@@ -204,6 +210,7 @@ Daemon that starts drivers based on found I2C devices.
|
|||||||
PRINT_MODULE_USAGE_NAME("i2c_launcher", "system");
|
PRINT_MODULE_USAGE_NAME("i2c_launcher", "system");
|
||||||
PRINT_MODULE_USAGE_COMMAND("start");
|
PRINT_MODULE_USAGE_COMMAND("start");
|
||||||
PRINT_MODULE_USAGE_PARAM_INT('b', 0, 1, 4, "Bus number", false);
|
PRINT_MODULE_USAGE_PARAM_INT('b', 0, 1, 4, "Bus number", false);
|
||||||
|
PRINT_MODULE_USAGE_PARAM_INT('t', 1, 1, 3, "battery index for calibration values (1 or 3)", false);
|
||||||
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
|
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -215,18 +222,23 @@ extern "C" __EXPORT int i2c_launcher_main(int argc, char *argv[])
|
|||||||
|
|
||||||
static I2CLauncher* instances[I2C_BUS_MAX_BUS_ITEMS];
|
static I2CLauncher* instances[I2C_BUS_MAX_BUS_ITEMS];
|
||||||
int bus = -1;
|
int bus = -1;
|
||||||
|
int batt_index = -1;
|
||||||
int myoptind = 1;
|
int myoptind = 1;
|
||||||
int ch;
|
int ch;
|
||||||
const char *myoptarg = nullptr;
|
const char *myoptarg = nullptr;
|
||||||
|
|
||||||
const char *verb = argv[1];
|
const char *verb = argv[1];
|
||||||
|
|
||||||
while ((ch = px4_getopt(argc, argv, "b:", &myoptind, &myoptarg)) != EOF) {
|
while ((ch = px4_getopt(argc, argv, "b:t:", &myoptind, &myoptarg)) != EOF) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'b':
|
case 'b':
|
||||||
bus = strtol(myoptarg, nullptr, 10);
|
bus = strtol(myoptarg, nullptr, 10);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 't':
|
||||||
|
batt_index = strtol(myoptarg, nullptr, 10);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return ThisDriver::print_usage("unrecognized flag");
|
return ThisDriver::print_usage("unrecognized flag");
|
||||||
}
|
}
|
||||||
@@ -250,7 +262,7 @@ extern "C" __EXPORT int i2c_launcher_main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (strcmp(verb, "start") == 0) {
|
if (strcmp(verb, "start") == 0) {
|
||||||
|
|
||||||
instances[bus] = new I2CLauncher(bus);
|
instances[bus] = new I2CLauncher(bus, batt_index);
|
||||||
|
|
||||||
if (instances[bus]) {
|
if (instances[bus]) {
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ using namespace time_literals;
|
|||||||
class I2CLauncher : public ModuleBase<I2CLauncher>, public ModuleParams, public px4::ScheduledWorkItem
|
class I2CLauncher : public ModuleBase<I2CLauncher>, public ModuleParams, public px4::ScheduledWorkItem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
I2CLauncher(int bus);
|
I2CLauncher(int bus, int batt_index);
|
||||||
|
|
||||||
~I2CLauncher() override;
|
~I2CLauncher() override;
|
||||||
|
|
||||||
@@ -78,11 +78,12 @@ private:
|
|||||||
|
|
||||||
void Run() override;
|
void Run() override;
|
||||||
|
|
||||||
static void scan_i2c_bus(int bus);
|
static void scan_i2c_bus(int bus, int batt_index);
|
||||||
|
|
||||||
uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)};
|
uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)};
|
||||||
uORB::Subscription _vehicle_status_sub{ORB_ID(vehicle_status)}; // regular subscription for additional data
|
uORB::Subscription _vehicle_status_sub{ORB_ID(vehicle_status)}; // regular subscription for additional data
|
||||||
|
|
||||||
int _bus;
|
int _bus;
|
||||||
|
int _batt_index;
|
||||||
bool _armed {false};
|
bool _armed {false};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user