Skip to content

Commit

Permalink
First working prototype
Browse files Browse the repository at this point in the history
Awesome: charging path found!
total time, including charging: 3.71711
go to: "BRCC - Village-relais de Yamachiche" 1.24167
go to: "MTQ - Trois-Rivières (Cap-de-la-Madeleine)" 0.583167
go to: "MTQ - Restaurant Le Chavigny - Deschambault" 0.919
go to destination 0.973278

Signed-off-by: Francis Giraldeau <[email protected]>
  • Loading branch information
giraldeau committed May 12, 2016
1 parent 88828df commit fcb0d0e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
6 changes: 4 additions & 2 deletions evnav/charger.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <QJsonArray>
#include <osrm/coordinate.hpp>

#include "edge.h"

/*
{
"AvailableCount": null,
Expand Down Expand Up @@ -37,14 +39,14 @@ class Charger
public:
Charger();
QString name() { return m_name; }
int id() { return m_id; }
VertexId id() { return m_id; }
osrm::Coordinate loc() { return m_loc; }
int network_id() { return m_network_id; }
int level() { return m_level; }
void read(const QJsonObject &json);

private:
int m_id;
VertexId m_id;
QString m_name;
osrm::Coordinate m_loc;
int m_level;
Expand Down
10 changes: 5 additions & 5 deletions evnav/chargerprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ChargerProvider::ChargerProvider()
{
}

ChargerProvider::ChargerProvider(QVector<Charger> &lst)
ChargerProvider::ChargerProvider(QMap<VertexId, Charger> &lst)
{
m_chargers = lst;
}
Expand All @@ -19,7 +19,7 @@ void ChargerProvider::read(const QJsonArray &json)
QJsonObject obj = json[i].toObject();
Charger c;
c.read(obj);
m_chargers.append(c);
m_chargers[c.id()] = c;
}
}

Expand All @@ -35,10 +35,10 @@ void ChargerProvider::loadJson(QString path)

ChargerProvider ChargerProvider::filter(std::function<bool (Charger &)> predicate)
{
QVector<Charger> filtered;
for (Charger &c: m_chargers) {
QMap<VertexId, Charger> filtered;
for (Charger &c: m_chargers.values()) {
if (predicate(c)) {
filtered.append(c);
filtered[c.id()] = c;
}
}
return ChargerProvider(filtered);
Expand Down
8 changes: 5 additions & 3 deletions evnav/chargerprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <osrm/coordinate.hpp>

#include <functional>
#include "edge.h"
#include "charger.h"

using namespace osrm;
Expand All @@ -17,12 +18,13 @@ class ChargerProvider
{
public:
ChargerProvider();
ChargerProvider(QVector<Charger> &lst);
ChargerProvider(QMap<VertexId, Charger> &lst);

void read(const QJsonArray &json);
void loadJson(QString path);
int size() { return m_chargers.size(); }
QVector<Charger>& chargers() { return m_chargers; }
QList<Charger> chargers() { return m_chargers.values(); }
Charger charger(VertexId id) { return m_chargers[id]; }


ChargerProvider filter(std::function<bool (Charger &)> predicate);
Expand All @@ -37,7 +39,7 @@ public slots:


private:
QVector<Charger> m_chargers;
QMap<VertexId, Charger> m_chargers;
};

#endif // CHARGERPROVIDER_H
31 changes: 23 additions & 8 deletions evnav/evnav.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <QElapsedTimer>
#include "evnav.h"
#include "graph.h"
#include "shortestpath.h"

#define _USE_MATH_DEFINES
#include <cmath>
Expand Down Expand Up @@ -205,16 +206,14 @@ void Evnav::route(Coordinate &src, Coordinate &dst)
}
double e = computeEnergy(t, eff);
if (e < (batt * SOC_dyn)) {
//double charge_time = computeChargingTime(e, power);
double total_time = computeTripTimeWithCharging(trip, e, power);
double charge_time = computeChargingTime(e, power);
double total_time = computeTripTimeWithCharging(t, e, power);
Edge edge{a.id(), b.id(), total_time};
qDebug() << a.name() << " -> " << b.name();
/*
qDebug() << a.name() << " -> " << b.name()
<< "distance:" << (t.dist_m / 1000.0)
<< "travel time:" << (t.time_s / 3600.0)
<< "charge time:" << (charge_time / 3600.0)
<< "total time :" << (total_time / 3600.0);
*/
g.addEdge(edge);
}
});
Expand All @@ -233,10 +232,26 @@ void Evnav::route(Coordinate &src, Coordinate &dst)
}

qDebug() << "graph size:" << g.E();

// TODO: write the graph as Json
// TODO: write the path as Json

ShortestPath sp(g, srcId);
if (sp.hasPathTo(dstId)) {
QVector<Edge> path = sp.pathTo(dstId).toVector();
if (path.isEmpty()) {
qDebug() << "error: found path, but path is empty";
return;
}
qDebug() << "Awesome: charging path found!";
qDebug() << "total time, including charging:" << sp.distTo(dstId) / 3600;
for (int i = 0; i < path.size() - 1; i++) {
Edge &e = path[i];
qDebug() << "go to:" << m_provider.charger(e.to()).name() << (e.weight() / 3600);
}
qDebug() << "go to destination" << (path.last().weight() / 3600);

// compute the shortest path
// compute detail of the trip
} else {
qDebug() << "cannot reach destination with this electric car";
}

}

0 comments on commit fcb0d0e

Please sign in to comment.