-
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 1 commit
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
//QT | ||
#include <QQuickPaintedItem> | ||
#include <QPainter> | ||
#include <QObject> | ||
|
||
//ROS | ||
#include <ros/ros.h> | ||
|
@@ -21,7 +20,7 @@ class ROS_Battery_Indicator: public QQuickPaintedItem { | |
// Constructor, takes parent widget, which defaults to null | ||
ROS_Battery_Indicator(QQuickItem * parent = 0); | ||
|
||
//void paint(QPainter *painter); | ||
void paint(QPainter *painter); | ||
void setup(ros::NodeHandle * nh); | ||
|
||
//getters and setters | ||
|
@@ -40,7 +39,7 @@ class ROS_Battery_Indicator: public QQuickPaintedItem { | |
QString topic_value; | ||
bool ros_ready; | ||
|
||
int charge; //the battery charge | ||
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 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 |
||
}; | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,74 @@ | ||
#include "ros_video_components/ros_battery_indicator.hpp" //added | ||
#include "ros_video_components/ros_battery_indicator.hpp" | ||
|
||
#define RECT_X 10 | ||
#define RECT_Y 10 | ||
#define FULL_CHARGE 33 | ||
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. FULL_CHARGE is a percentage now (100) |
||
#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/signal"), | ||
topic_value("/rover/batttery"), | ||
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. hash define topic in header |
||
ros_ready(false), | ||
charge(33) { | ||
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. change this to a percentage out of 100 |
||
} | ||
|
||
void ROS_Battery_Indicator::setup(ros::NodeHandle * nh) { | ||
|
||
signal_sub = nh->subscribe( | ||
"/rover/signal", //TODO | ||
"/rover/battery", //TODO | ||
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. topic in header |
||
1, | ||
&ROS_Battery_Indicator::receive_signal, | ||
this | ||
); | ||
ros_ready = true; | ||
} | ||
|
||
/*void ROS_Battery_Indicator::paint(QPainter * painter) { | ||
int x = 20; | ||
int y = 20; | ||
painter->drawRect(x,y,FULL_CHARGE,15); | ||
void ROS_Battery_Indicator::paint(QPainter * painter) { | ||
|
||
}*/ | ||
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"); | ||
|
@@ -44,16 +87,14 @@ void ROS_Battery_Indicator::set_topic(const QString & new_value) { | |
} | ||
} | ||
|
||
QString battery_indicator::get_topic() const { | ||
QString ROS_Battery_Indicator::get_topic() const { | ||
return topic_value; | ||
} | ||
|
||
void battery_indicator:: | ||
void ROS_Battery_Indicator:: | ||
receive_signal(const std_msgs::Float32::ConstPtr & msg) { | ||
charge = msg->charge; | ||
ROS_INFO("Received signal message"); | ||
charge = msg->data; | ||
ROS_INFO("Received battery message"); | ||
update(); | ||
} | ||
|
||
ROS_Battery_Indicator * ROS_Battery_Indicator::instance = NULL; | ||
|
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.
Remove commented code