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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
308 changes: 308 additions & 0 deletions qt-gui.workspace.user.865c78c

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions src/gui/resources/main_window.qml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import QtQuick 2.5
import QtQuick.Window 2.2
import bluesat.owr 1.0
//import bluesat.owr.singleton 1.0



Window {
id: main_window
width: 800
height: 800
opacity: 1
title: "BLUEsat OWR"
visible: true
minimumHeight: 600
Expand Down Expand Up @@ -71,6 +73,36 @@ Window {

}

Item {
id: battery_indicator_container
z: 5
anchors.right: parent.right
anchors.rightMargin: 0
anchors.top: parent.top
anchors.topMargin: 0
anchors.left: parent.right
anchors.leftMargin: -172
anchors.bottom: parent.top
anchors.bottomMargin: -100
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")
}
}

Item {
id: signal_strength_container
z: 4
Expand Down
5 changes: 5 additions & 0 deletions src/gui/src/main_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <QDebug>
#include "ros_video_components/ros_video_component.hpp"
#include "ros_video_components/ros_signal_strength.hpp"
#include "ros_video_components/ros_battery_indicator.hpp" //added
Copy link
Contributor

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 😛

#include "ros_video_components/ros_voltage_meter.hpp"
#include "ros_video_components/ros_camera_switching.hpp"
#include "ros_video_components/ros_timer.hpp"
Expand All @@ -14,6 +15,7 @@ Main_Application::Main_Application() {}
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
qmlRegisterType<ROS_Voltage_Meter>("bluesat.owr", 1, 0, "ROSVoltageMeter");
qmlRegisterType<ROS_Camera_Switching>("bluesat.owr", 1, 0, "ROSCameraSwitching");
qmlRegisterType<ROSTimer>("bluesat.owr", 1, 0, "ROSTimer");
Expand All @@ -36,6 +38,9 @@ void Main_Application::run() {
ROS_Signal_Strength * signal_strength = this->rootObjects()[0]->findChild<ROS_Signal_Strength*>(QString("signal_strength"));
signal_strength->setup(&nh);

ROS_Battery_Indicator * battery_indicator = this->rootObjects()[0]->findChild<ROS_Battery_Indicator*>(QString("battery_indicator"));
battery_indicator->setup(&nh); //added

ROS_Voltage_Meter * voltage_meter = this->rootObjects()[0]->findChild<ROS_Voltage_Meter*>(QString("voltage_meter"));
voltage_meter->setup(&nh);

Expand Down
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 {
Copy link
Member

Choose a reason for hiding this comment

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

This can probably just extend QQuickItem as it doesn't need the functionality of QQuickPaintedItem

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

Choose a reason for hiding this comment

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

receive_signal is not a proper variable name here. I named it receive_signal because it was literally receiving the signal strength of the antenna. For you, it would be something like receive_battery_voltage.


// ROS
ros::NodeHandle * nh;
ros::Subscriber signal_sub;
Copy link
Contributor

Choose a reason for hiding this comment

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

signal_sub, again, is not an appropriate name here. I would suggest battery_sub or something.

QString topic_value;
bool ros_ready;

float charge; //the battery charge
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

};


#endif // BATTERY_INDICATOR_HPP

2 changes: 2 additions & 0 deletions src/ros_video_components/src/owr_ros_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ros_video_components/owr_ros_components.hpp"
#include "ros_video_components/ros_video_component.hpp"
#include "ros_video_components/ros_signal_strength.hpp"
#include "ros_video_components/ros_battery_indicator.hpp"
#include "ros_video_components/ros_voltage_meter.hpp"
#include "ros_video_components/ros_camera_switching.hpp"
#include "ros_video_components/ros_timer.hpp"
Expand All @@ -17,6 +18,7 @@
void OWR_ROS_Components::registerTypes(const char *uri) {
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
qmlRegisterType<ROS_Voltage_Meter>("bluesat.owr", 1, 0, "ROSVoltageMeter");
qmlRegisterType<ROS_Camera_Switching>("bluesat.owr", 1, 0, "ROSCameraSwitching");
qmlRegisterType<ROSTimer>("bluesat.owr", 1, 0, "ROSTimer");
Expand Down
100 changes: 100 additions & 0 deletions src/ros_video_components/src/ros_battery_indicator.cpp
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);
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");
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();
}