-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from StanfordASL/alvin/binary_thrust_driver
change driver to work with binary thrust message
- Loading branch information
Showing
7 changed files
with
268 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Stanford Autonomous Systems Lab | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#pragma once | ||
|
||
#include <fstream> | ||
#include <string> | ||
|
||
namespace ff | ||
{ | ||
|
||
class GPIO | ||
{ | ||
public: | ||
explicit GPIO(int pin); | ||
~GPIO(); | ||
|
||
void SetState(bool state); | ||
void SetPolarity(bool normal = true); | ||
bool GetState() const; | ||
|
||
private: | ||
int pin_; | ||
bool state_; | ||
std::ofstream f_value_; | ||
std::ofstream f_active_low_; | ||
|
||
std::string BasePath() const; | ||
}; | ||
|
||
} // namespace ff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Stanford Autonomous Systems Lab | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#include "ff_drivers/gpio.hpp" | ||
|
||
namespace ff | ||
{ | ||
|
||
GPIO::GPIO(int pin) | ||
: pin_(pin) | ||
{ | ||
// request gpio device | ||
{ | ||
std::ofstream f_export("/sys/class/gpio/export"); | ||
f_export << pin; | ||
} | ||
|
||
// configure GPIO to be an output | ||
{ | ||
std::ofstream f_direction(BasePath() + "direction"); | ||
f_direction << "out"; | ||
} | ||
|
||
f_value_.open(BasePath() + "value"); | ||
f_active_low_.open(BasePath() + "active_low"); | ||
|
||
SetPolarity(true); | ||
SetState(false); | ||
} | ||
|
||
GPIO::~GPIO() | ||
{ | ||
SetState(false); | ||
|
||
// close files | ||
f_value_.close(); | ||
f_active_low_.close(); | ||
|
||
// free gpio device | ||
std::ofstream f_unexport("/sys/class/gpio/unexport"); | ||
f_unexport << pin_; | ||
} | ||
|
||
void GPIO::SetState(bool state) | ||
{ | ||
f_value_ << static_cast<int>(state); | ||
f_value_.flush(); | ||
state_ = state; | ||
} | ||
|
||
bool GPIO::GetState() const | ||
{ | ||
return state_; | ||
} | ||
|
||
void GPIO::SetPolarity(bool normal) | ||
{ | ||
f_active_low_ << static_cast<int>(!normal); | ||
f_active_low_.flush(); | ||
} | ||
|
||
std::string GPIO::BasePath() const | ||
{ | ||
return "/sys/class/gpio/gpio" + std::to_string(pin_) + "/"; | ||
} | ||
|
||
} // namespace ff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2023 Stanford Autonomous Systems Lab | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#include <chrono> | ||
#include <memory> | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
|
||
#include "ff_drivers/pwm.hpp" | ||
#include "ff_msgs/msg/thruster_pwm_command.hpp" | ||
|
||
#define NUM_THRUSTERS 8 | ||
|
||
// thruster pin connection | ||
// @see: https://wiki.odroid.com/odroid-n2l/application_note/gpio/pwm#tab__odroid-n2 | ||
static constexpr int THRUSTER_PINS[NUM_THRUSTERS] = { | ||
476, // thruster pin 1 -> odroid pin 16 | ||
477, // thruster pin 2 -> odroid pin 18 | ||
484, // thruster pin 3 -> odroid pin 19 | ||
485, // thruster pin 4 -> odroid pin 21 | ||
478, // thruster pin 5 -> odroid pin 22 | ||
487, // thruster pin 6 -> odroid pin 23 | ||
486, // thruster pin 7 -> odroid pin 24 | ||
464, // thruster pin 8 -> odroid pin 26 | ||
}; | ||
|
||
|
||
using namespace std::chrono_literals; | ||
using ff_msgs::msg::ThrusterPWMCommand; | ||
|
||
class ThrusterNode : public ff::PWMManager | ||
{ | ||
public: | ||
ThrusterNode() | ||
: ff::PWMManager("thruster_driver_node") | ||
{ | ||
// add all PWMs | ||
for (size_t i = 0; i < NUM_THRUSTERS; ++i) { | ||
this->AddSoftPWM(THRUSTER_PINS[i]); | ||
} | ||
|
||
// set period (default to 10Hz) | ||
double period = this->declare_parameter("period", .1); | ||
this->SetPeriodAll(period * 1s); | ||
// update period on the fly | ||
sub_params_ = std::make_shared<rclcpp::ParameterEventHandler>(this); | ||
cb_period_ = sub_params_->add_parameter_callback( | ||
"period", | ||
[this](const rclcpp::Parameter & p) {SetPeriodAll(p.as_double() * 1s);}); | ||
|
||
// start all PWMs | ||
this->EnableAll(); | ||
|
||
// listen to commands | ||
sub_duty_cycle_ = this->create_subscription<ThrusterPWMCommand>( | ||
"commands/duty_cycle", | ||
10, [this](const ThrusterPWMCommand::SharedPtr msg) {DutyCycleCallback(msg);}); | ||
} | ||
|
||
private: | ||
std::shared_ptr<rclcpp::ParameterEventHandler> sub_params_; | ||
std::shared_ptr<rclcpp::ParameterCallbackHandle> cb_period_; | ||
rclcpp::Subscription<ThrusterPWMCommand>::SharedPtr sub_duty_cycle_; | ||
|
||
void DutyCycleCallback(const ThrusterPWMCommand::SharedPtr msg) | ||
{ | ||
for (size_t i = 0; i < NUM_THRUSTERS; ++i) { | ||
this->SetDutyCycle(i, msg->duty_cycles[i]); | ||
} | ||
} | ||
}; | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
rclcpp::init(argc, argv); | ||
rclcpp::spin(std::make_shared<ThrusterNode>()); | ||
rclcpp::shutdown(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters