Skip to content
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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion qt-gui.workspace.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.2.1, 2018-06-09T13:50:58. -->
<!-- Written by QtCreator 4.2.1, 2018-08-28T00:13:24. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
34 changes: 32 additions & 2 deletions src/gui/resources/main_window.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,37 @@ Window {
minimumWidth: 600

//Battery indicator
Rectangle {
Item {
id: battery_indicator_container
anchors.left: parent.left
anchors.rightMargin: 76
anchors.top: parent.top
anchors.topMargin: 0
anchors.right: logo.left
anchors.leftMargin: 0
//anchors.bottom: videoStream.top
anchors.bottom: video_pane.top
anchors.bottomMargin: 90
ROSBatteryIndicator {
// @disable-check M16
objectName: "battery_indicator"
id: battery_indicator
// @disable-check M16`
anchors.bottom: parent.bottom
// @disable-check M16
anchors.bottomMargin: 0
// @disable-check M16
anchors.top: parent.top
// @disable-check M16
anchors.left: parent.left
// @disable-check M16
anchors.right: parent.right
// @disable-check M16
topic: qsTr("/rover/signal")
}
}

/*Rectangle {
id: battery_indicator
property var charge: 33
property var full_charge: 33
Expand Down Expand Up @@ -82,7 +112,7 @@ Window {


}
}
}*/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code

Image {
id: logo
source: "/images/bluesatLogo.png"
Expand Down
1 change: 0 additions & 1 deletion src/gui/src/main_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ void Main_Application::run() {
qmlRegisterType<ROS_Video_Component>("bluesat.owr", 1, 0, "ROSVideoComponent");
qmlRegisterType<ROS_Signal_Strength>("bluesat.owr", 1, 0, "ROSSignalStrength");
qmlRegisterType<ROS_Battery_Indicator>("bluesat.owr", 1, 0, "ROSBatteryIndicator"); //added
//qmlRegisterSingletonType<ROS_Battery_Indicator>("bluesat.owr.singleton", 1, 0, "ROSBatterryIndicator", &ros_battery_indicator::qml_instance);

// this loads the qml file we are about to create
this->load(QUrl(QStringLiteral("qrc:/main_window.qml")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//QT
#include <QQuickPaintedItem>
#include <QPainter>
#include <QObject>

//ROS
#include <ros/ros.h>
Expand All @@ -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
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is off

};


Expand Down
69 changes: 55 additions & 14 deletions src/ros_video_components/src/ros_battery_indicator.cpp
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
Copy link

Choose a reason for hiding this comment

The 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"),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hash define topic in header

ros_ready(false),
charge(33) {
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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);
Copy link

Choose a reason for hiding this comment

The 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");
Expand All @@ -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;