-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sv 4 battery indicator #9
base: master
Are you sure you want to change the base?
Changes from all commits
d940aaf
47fac4c
67a3414
102a130
6b01515
e7717c2
671ca5b
02395ca
4f5c860
d3e2718
daa822a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef ROS_BATTERY_INDICATOR_HPP | ||
#define ROS_BATTERY_INDICATOR_HPP | ||
|
||
//QT | ||
#include <QQuickPaintedItem> | ||
#include <QPainter> | ||
|
||
//ROS | ||
#include <ros/ros.h> | ||
#include <std_msgs/Float32.h> | ||
|
||
|
||
class ROS_Battery_Indicator: public QQuickPaintedItem { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can probably just extend |
||
//make this a Qt Widget | ||
Q_OBJECT | ||
// defines a qml value for the topic | ||
Q_PROPERTY(QString topic READ get_topic WRITE set_topic NOTIFY topic_changed) | ||
|
||
public: | ||
// Constructor, takes parent widget, which defaults to null | ||
ROS_Battery_Indicator(QQuickItem * parent = 0); | ||
|
||
void paint(QPainter *painter); | ||
void setup(ros::NodeHandle * nh); | ||
|
||
//getters and setters | ||
void set_topic(const QString &new_value); | ||
QString get_topic() const; | ||
|
||
signals: | ||
void topic_changed(); | ||
|
||
private: | ||
void receive_signal(const std_msgs::Float32::ConstPtr & msg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// ROS | ||
ros::NodeHandle * nh; | ||
ros::Subscriber signal_sub; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
QString topic_value; | ||
bool ros_ready; | ||
|
||
float charge; //the battery charge | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation is off |
||
}; | ||
|
||
|
||
#endif // BATTERY_INDICATOR_HPP | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include "ros_video_components/ros_battery_indicator.hpp" | ||
|
||
#define RECT_X 10 | ||
#define RECT_Y 10 | ||
#define FULL_CHARGE 33 | ||
#define GREEN (FULL_CHARGE / 3) * 2 | ||
#define YELLOW FULL_CHARGE / 3 | ||
#define INDICATOR_HEIGHT 15 | ||
#define NOB_X RECT_X + FULL_CHARGE | ||
#define NOB_Y RECT_Y + (RECT_Y / 2) | ||
#define NOB_WIDTH 4 | ||
#define NOB_HEIGHT 5 | ||
#define TEXT_X RECT_X + FULL_CHARGE + ((RECT_X/2) + 1) | ||
#define TEXT_Y RECT_Y + 12 | ||
#define BAR_WIDTH 1 | ||
|
||
ROS_Battery_Indicator::ROS_Battery_Indicator(QQuickItem * parent) : | ||
QQuickPaintedItem(parent), | ||
topic_value("/rover/batttery"), | ||
ros_ready(false), | ||
charge(33) { | ||
} | ||
|
||
void ROS_Battery_Indicator::setup(ros::NodeHandle * nh) { | ||
|
||
signal_sub = nh->subscribe( | ||
"/rover/battery", //TODO | ||
1, | ||
&ROS_Battery_Indicator::receive_signal, | ||
this | ||
); | ||
ros_ready = true; | ||
} | ||
|
||
void ROS_Battery_Indicator::paint(QPainter * painter) { | ||
painter->setPen(Qt::white); | ||
painter->drawRect(RECT_X, RECT_Y, FULL_CHARGE, INDICATOR_HEIGHT); //Draws outer rectangle | ||
QLinearGradient linearGradient(0, 0, 100, 100); | ||
linearGradient.setColorAt(0.0, Qt::black); | ||
painter->setBrush(linearGradient); | ||
painter->drawRect(NOB_X, NOB_Y, NOB_WIDTH, NOB_HEIGHT); //Draws it at the end of the rectangle | ||
|
||
float charge_p = charge; //Done to easily be able to adjust values of charge from subscriber | ||
float bat = 0; //Used to limit how far the battery meter can if there is too much charge | ||
if (charge_p > FULL_CHARGE) { | ||
linearGradient.setColorAt(0.0, Qt::red); | ||
bat = FULL_CHARGE; | ||
} else if (charge_p > GREEN) { | ||
linearGradient.setColorAt(0.0, Qt::green); | ||
bat = charge_p; | ||
} else if (charge_p > YELLOW) { | ||
linearGradient.setColorAt(0.0, Qt::yellow); | ||
bat = charge_p; | ||
} else { | ||
linearGradient.setColorAt(0.0, Qt::red); | ||
bat = charge_p; | ||
} | ||
painter->setBrush(linearGradient); //Setting the right color | ||
painter->drawRect(RECT_X, RECT_Y, bat, INDICATOR_HEIGHT); //Draws bar showing charge | ||
|
||
linearGradient.setColorAt(0.0, Qt::black); | ||
painter->setBrush(linearGradient); | ||
painter->drawRect(RECT_X + YELLOW, RECT_Y, 1, INDICATOR_HEIGHT); //Draws a bar on the indicator | ||
painter->drawRect(RECT_X + GREEN, RECT_Y, 1, INDICATOR_HEIGHT); //Draws a bar on the indicator | ||
|
||
char percentage[6]; | ||
charge_p = (charge_p / 33) * 100; | ||
sprintf(percentage, "%d%%", (int) charge_p); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. charge is already a percentage, no need for another variable |
||
percentage[5] = '\0'; | ||
painter->drawText(TEXT_X, TEXT_Y, percentage); //Draws battery percentage | ||
} | ||
|
||
void ROS_Battery_Indicator::set_topic(const QString & new_value) { | ||
ROS_INFO("set_topic"); | ||
if(topic_value != new_value) { | ||
topic_value = new_value; | ||
if(ros_ready) { | ||
signal_sub.shutdown(); | ||
signal_sub = nh->subscribe( | ||
topic_value.toStdString(), //TODO | ||
1, | ||
&ROS_Battery_Indicator::receive_signal, | ||
this | ||
); | ||
} | ||
emit topic_changed(); | ||
} | ||
} | ||
|
||
QString ROS_Battery_Indicator::get_topic() const { | ||
return topic_value; | ||
} | ||
|
||
void ROS_Battery_Indicator:: | ||
receive_signal(const std_msgs::Float32::ConstPtr & msg) { | ||
charge = msg->data; | ||
ROS_INFO("Received battery message"); | ||
update(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need the comment
//added
😛