forked from KumarRobotics/kr_mav_control
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
41 additions
and
3 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
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,35 @@ | ||
import rospy | ||
from pymavlink import mavutil | ||
from sensor_msgs.msg import BatteryState | ||
|
||
def mavlink_connect(port='/dev/ttyUSB0', baudrate=115200): | ||
"""Connect to a MAVLink source""" | ||
return mavutil.mavlink_connection(port, baud=baudrate) | ||
|
||
def battery_status_publisher(): | ||
rospy.init_node('battery_status_publisher', anonymous=True) | ||
battery_pub = rospy.Publisher('battery_status', BatteryState, queue_size=10) | ||
rate = rospy.Rate(100) | ||
|
||
# Connect to MAVLink (adjust port and baudrate as necessary) | ||
mav = mavlink_connect(port='/dev/ttyUSB0', baudrate=115200) | ||
|
||
while not rospy.is_shutdown(): | ||
message = mav.recv_match(blocking=True) | ||
if message: | ||
if message.get_type() == "SYS_STATUS": | ||
battery_msg = BatteryState() | ||
battery_msg.voltage = message.voltage_battery / 1000.0 / 4.0 | ||
battery_msg.current = message.current_battery / 100.0 | ||
battery_msg.percentage = message.battery_remaining | ||
|
||
battery_msg.header.stamp = rospy.Time.now() | ||
battery_pub.publish(battery_msg) | ||
|
||
rate.sleep() | ||
|
||
if __name__ == '__main__': | ||
try: | ||
battery_status_publisher() | ||
except rospy.ROSInterruptException: | ||
pass |
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