Unverified Commit e919502b authored by Jeff Niu's avatar Jeff Niu Committed by GitHub

Merge pull request #60 from aneedalie/sam-controls

Solenoid controller for SAM
parents d38a05e2 2b0aece6
......@@ -123,9 +123,9 @@ void Actuator::move(Vector2i dir, int timer) {
success = false;
}
catch (...) {
success = false;
#ifndef NDEBUG
Logger::log("Unexpected exception", Logger::DEBUG);
success = false;
#endif
}
......
......@@ -19,6 +19,7 @@ public:
enum Type {
ACTUATOR,
SIMULATOR,
SOLENOID,
NUM_DELGATES
};
......
#include "solenoid.h"
#include <ostream>
Solenoid::Solenoid(const QString& serial_port): Controller(1, 1) {
// TODO: change actuator setup to be used for general serial set up
if ( serial_port.isEmpty() ) {
// TODO: provide error if this does not succeed
stream.open("/dev/ttyACM0"); // manually changed to match port indicated in arduino IDE
} else {
stream.open(serial_port.toStdString());
}
}
Solenoid::~Solenoid() {
stream.close();
}
void Solenoid::move(Vector2i dir, int timer) {
// TODO: user timer for movement control
bool success = true;
#ifndef NDEBUG
Logger::log("Attempting move (" + std::to_string(dir.x_comp) + ", " + std::to_string(dir.y_comp) + ")",
Logger::DEBUG);
#endif
try {
stream << vectorToBinary(dir);
stream.flush();
} catch (...) {
success = false;
#ifndef NDEBUG
Logger::log("Unexpected exception", Logger::DEBUG);
#endif
}
if (success) {
Logger::log("Moved " + dir.toString() + " in " + std::to_string(timer) + " milliseconds.", Logger::INFO);
} else {
Logger::log("The movement " + dir.toString() + " could not be completed.", Logger::FATAL);
}
}
int Solenoid::vectorToBinary(Vector2i dir) {
int direction = 0b0;
switch(dir.x_comp) {
case 1:
direction = direction | Direction::UP;
break;
case -1:
direction = direction | Direction::DOWN;
break;
case 0:
// DO NOTHING
break;
default:
Logger::log("Invalid direction x component", Logger::FATAL);
}
switch(dir.y_comp) {
case 1:
direction = direction | Direction::RIGHT;
break;
case -1:
direction = direction | Direction::LEFT;
break;
case 0:
// DO NOTHING
break;
default:
Logger::log("Invalid direction y component", Logger::FATAL);
}
return direction;
}
#ifndef SOLENOID_H
#define SOLENOID_H
#include "controller.h"
#include <fstream>
class Solenoid : public Controller {
public:
Solenoid(const QString& serial_port = "");
~Solenoid();
void move(Vector2i dir, int timer);
private:
enum Direction { UP = 0b1000, RIGHT = 0b0100, DOWN = 0b0010, LEFT = 0b0001 };
std::ofstream stream;
int vectorToBinary(Vector2i dir);
};
#endif // SOLENOID_H
......@@ -12,8 +12,9 @@ MainWindow::MainWindow(QWidget *parent, const char *) :
//Set up logger
Logger::setStream(getLogView());
m_actuator = std::shared_ptr<Actuator>(new Actuator);
m_solenoid = std::shared_ptr<Solenoid>(new Solenoid);
m_simulator = std::shared_ptr<Simulator>(new Simulator(1, -1));
m_controller = m_actuator;
m_controller = m_solenoid;
m_controller_type = Controller::Type::ACTUATOR;
// Bind controller to Python Engine
......
......@@ -9,6 +9,7 @@
#include <unordered_map>
#include "../controller/controller.h"
#include "../controller/solenoid.h"
#include "../controller/simulator.h"
#include "actuatorsetup.h"
......@@ -65,6 +66,7 @@ private:
ActionAbout *action_about_window;
std::shared_ptr<Controller> m_controller;
std::shared_ptr<Actuator> m_actuator;
std::shared_ptr<Solenoid> m_solenoid;
std::shared_ptr<Simulator> m_simulator;
bool eventFilter(QObject *, QEvent *) override;
......
......@@ -62,7 +62,7 @@ RenderScene::center() const {
return {width() / 2.0f, height() / 2.0f};
}
const std::vector<Solenoid> *RenderScene::solenoids() const {
const std::vector<SimulatorSolenoid> *RenderScene::solenoids() const {
return &m_solenoids;
}
......
......@@ -10,7 +10,7 @@
#include "../controller/simulator.h"
#include "renderscenebase.h"
#include "drawable.h"
#include "solenoid.h"
#include "simulatorSolenoid.h"
#include "sam.h"
/**
......@@ -60,7 +60,7 @@ public:
/**
* @return a vector of solenoids in the field
*/
const std::vector<Solenoid> *solenoids() const override;
const std::vector<SimulatorSolenoid> *solenoids() const override;
/**
* @return a pointer to the Sam instance.
......@@ -94,7 +94,7 @@ private:
/**
* List of solenoids in the field.
*/
std::vector<Solenoid> m_solenoids;
std::vector<SimulatorSolenoid> m_solenoids;
/**
* Object representing the location of SAM.
*/
......
......@@ -10,7 +10,7 @@
#include "drawable.h"
class Solenoid;
class SimulatorSolenoid;
class SAMRobot;
/**
......@@ -22,7 +22,7 @@ public:
typedef typename Drawable::vector2f vector2f;
virtual vector2f center() const = 0;
virtual const std::vector<Solenoid> *solenoids() const = 0;
virtual const std::vector<SimulatorSolenoid> *solenoids() const = 0;
virtual const SAMRobot *sam() const = 0;
};
......
#include "sam.h"
#include "solenoid.h"
#include "simulatorSolenoid.h"
SAMRobot::SAMRobot(RenderSceneBase *scene, float mu_s, float mass, float length)
: m_renderScene(scene),
......@@ -13,7 +13,7 @@ SAMRobot::SAMRobot(RenderSceneBase *scene, float mu_s, float mass, float length)
void SAMRobot::draw(QPainter *painter, QPaintEvent *, int elapsed, float scale) {
// Update phase
float dt = elapsed / 1000.0f;
const std::vector<Solenoid> *solenoids = m_renderScene->solenoids();
const std::vector<SimulatorSolenoid> *solenoids = m_renderScene->solenoids();
m_mag.setX(0);
m_mag.setY(0);
for (auto &solenoid : *solenoids) {
......
#include "solenoid.h"
#include "simulatorSolenoid.h"
Solenoid::Solenoid(const RenderSceneBase *scene,
SimulatorSolenoid::SimulatorSolenoid(const RenderSceneBase *scene,
const vector2f &P, float theta, float n, float R, float l, float mu_rel)
: m_renderScene(scene),
m_pos(P),
......@@ -12,19 +12,19 @@ Solenoid::Solenoid(const RenderSceneBase *scene,
m_I(0.0f) {
}
void Solenoid::setCurrent(float i) {
void SimulatorSolenoid::setCurrent(float i) {
m_I = i;
}
float Solenoid::getCurrent() {
float SimulatorSolenoid::getCurrent() {
return m_I;
}
inline float Solenoid::getA(float x, float y) const {
inline float SimulatorSolenoid::getA(float x, float y) const {
return powf((powf(x, 2.0f) + powf(y, 2.0f)), -1.5f);
}
void Solenoid::draw(QPainter *painter, QPaintEvent *, int, float scale) {
void SimulatorSolenoid::draw(QPainter *painter, QPaintEvent *, int, float scale) {
float s = sinf(m_theta);
float c = cosf(m_theta);
float l2 = m_len * scale / 2.0f;
......@@ -47,8 +47,8 @@ void Solenoid::draw(QPainter *painter, QPaintEvent *, int, float scale) {
painter->drawPolygon(vertices, 4);
}
typename Solenoid::vector2f
Solenoid::fieldAt(const vector2f &Q) const {
typename SimulatorSolenoid::vector2f
SimulatorSolenoid::fieldAt(const vector2f &Q) const {
vector2f Qp = rotate(translate(Q, m_pos), -m_theta);
float k = MU_0F * m_mu * m_I * m_N * powf(m_radius, 2.0f) / 4.0f;
float l2 = m_len / 2.0f;
......
......@@ -9,13 +9,13 @@
#include "renderscenebase.h"
/**
* Solenoid class represents an electromagnetic that is
* SimulatorSolenoid class represents an electromagnetic that is
* passed a current @code i @endcode to produce a magnetic
* field that moves SAM.
*/
class Solenoid : public Drawable {
class SimulatorSolenoid : public Drawable {
public:
Solenoid() = default;
SimulatorSolenoid() = default;
/**
* Create a solenoid.
*
......@@ -27,7 +27,7 @@ public:
* @param l the length of the solenoid
* @param mu_rel the relative permeability of the solenoid
*/
Solenoid(const RenderSceneBase *scene,
SimulatorSolenoid(const RenderSceneBase *scene,
const vector2f &P, float theta, float n, float R, float l, float mu_rel);
/**
......@@ -72,11 +72,11 @@ private:
*/
float m_N;
/**
* Solenoid radius in meters.
* SimulatorSolenoid radius in meters.
*/
float m_radius;
/**
* Solenoid length in meters.
* SimulatorSolenoid length in meters.
*/
float m_len;
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment