Commit 350793b0 authored by markus's avatar markus

Refactor Brake as semaphore

parent a42e2a62
#include "brake.h"
Brake::Brake(int brake_pin)
{
brake_counter_ = 0;
brake_pin_ = brake_pin;
//Pin Settings
pinMode(brake_pin_, OUTPUT);
//All Relais Off
digitalWrite(brake_pin_, HIGH);
}
void Brake::doBreak()
{
if(brake_counter_ == 0)
{
digitalWrite(brake_pin_, HIGH);
}
else
{
digitalWrite(brake_pin_, LOW);
}
}
void Brake::release()
{
brake_counter_++;
}
void Brake::tighten()
{
brake_counter_--;
}
#ifndef BRAKE_H
#define BRAKE_H
#include "settings.h"
#include "Arduino.h"
class Brake
{
public:
Brake(int brake_pin);
void doBreak();
void tighten();
void release();
private:
int brake_counter_;
int brake_pin_;
};
#endif //BRAKE_H
#include "motor.h"
Motor::Motor(double max, double min, int down_pin, int up_pin, int break_pin, int sensor_pin, double degree, double resolution, double span)
Motor::Motor(double max, double min, int down_pin, int up_pin, Brake* brake, int sensor_pin, double degree, double resolution, double span)
{
max_ = max;
min_ = min;
span_ = span;
brake_ = brake;
degree_ = degree;
up_pin_ = up_pin;
down_pin_ = down_pin;
break_pin_ = break_pin;
sensor_pin_ = sensor_pin;
resolution_ = resolution;
//Pin Settings
pinMode(up_pin_, OUTPUT);
pinMode(down_pin_, OUTPUT);
pinMode(break_pin_, OUTPUT);
//All Relais Off
digitalWrite(up_pin_, HIGH);
digitalWrite(down_pin_, HIGH);
digitalWrite(break_pin_, HIGH);
should_rotate_ = false;
brake_tightened_ = true;
}
void Motor::doMotor()
......@@ -45,7 +44,7 @@ void Motor::doMotor()
Motor::motorError Motor::rotateUp()
{
releaseBreak();
releaseBrake();
digitalWrite(down_pin_, HIGH);
digitalWrite(up_pin_, LOW);
return Motor::ROT_OK;
......@@ -53,26 +52,32 @@ Motor::motorError Motor::rotateUp()
Motor::motorError Motor::rotateDown()
{
releaseBreak();
releaseBrake();
digitalWrite(up_pin_, HIGH);
digitalWrite(down_pin_, LOW);
return Motor::ROT_OK;
}
void Motor::releaseBreak()
void Motor::releaseBrake()
{
digitalWrite(break_pin_, LOW);
if(brake_tightened_) {
brake_->release();
brake_tightened_ = false;
}
}
void Motor::tightenBreak()
void Motor::tightenBrake()
{
digitalWrite(break_pin_, HIGH);
if(brake_tightened_ == false) {
brake_->tighten();
brake_tightened_ = true;
}
}
void Motor::stop()
{
should_rotate_ = false;
tightenBreak();
tightenBrake();
digitalWrite(down_pin_, HIGH);
digitalWrite(up_pin_, HIGH);
}
......
......@@ -2,6 +2,7 @@
#define MOTOR_H
#include "Arduino.h"
#include "brake.h"
class Motor
{
......@@ -11,12 +12,12 @@ public:
ROT_VAL_OUT_OF_RANGE,
ROT_BREAK_NOT_RELEASED,
ROT_ALREADY_IN_SPAN };
Motor(double max, double min, int down_pin, int up_pin, int break_pin, int sensor_pin, double degree, double resolution, double span);
Motor(double max, double min, int down_pin, int up_pin, Brake *brake, int sensor_pin, double degree, double resolution, double span);
void doMotor();
double getPosition() const;
void releaseBreak();
void tightenBreak();
void releaseBrake();
void tightenBrake();
void stop();
motorError setPosition(double);
......@@ -27,11 +28,11 @@ private:
motorError readMotSensor();
double readADC(int);
Brake* brake_;
double max_;
double min_;
int down_pin_;
int up_pin_;
int break_pin_;
int sensor_pin_;
double position_;
double destination_;
......@@ -39,6 +40,7 @@ private:
double resolution_;
double span_;
byte should_rotate_;
byte brake_tightened_;
};
#endif //MOTOR_H
......@@ -2,34 +2,36 @@
Rotor::Rotor()
{
azimuth_ = new Motor(AZIM_MAX, AZIM_MIN, AZIM_RE_CW, AZIM_RE_CCW, AZIM_RE_BREAK, AZIM_POT, AZIM_DEG, AZIM_RES, AZIM_SPAN);
elevation_ = new Motor(ELEV_MAX, ELEV_MIN, ELEV_RE_DWN, ELEV_RE_UP, ELEV_RE_BREAK, ELEV_POT, ELEV_DEG, ELEV_RES, ELEV_SPAN);
brake_ = new Brake(AZIM_RE_BREAK);
azimuth_ = new Motor(AZIM_MAX, AZIM_MIN, AZIM_RE_CW, AZIM_RE_CCW, brake_, AZIM_POT, AZIM_DEG, AZIM_RES, AZIM_SPAN);
elevation_ = new Motor(ELEV_MAX, ELEV_MIN, ELEV_RE_DWN, ELEV_RE_UP, brake_, ELEV_POT, ELEV_DEG, ELEV_RES, ELEV_SPAN);
}
void Rotor::doRotor()
{
azimuth_->doMotor();
elevation_->doMotor();
brake_->doBreak();
}
void Rotor::releaseBreakAzimuth()
void Rotor::releaseBrakeAzimuth()
{
azimuth_->releaseBreak();
azimuth_->releaseBrake();
}
void Rotor::releaseBreakElevation()
void Rotor::releaseBrakeElevation()
{
elevation_->releaseBreak();
elevation_->releaseBrake();
}
void Rotor::tightenBreakAzimuth()
void Rotor::tightenBrakeAzimuth()
{
azimuth_->tightenBreak();
azimuth_->tightenBrake();
}
void Rotor::tightenBreakElevation()
void Rotor::tightenBrakeElevation()
{
elevation_->tightenBreak();
elevation_->tightenBrake();
}
void Rotor::stopAzimuth()
......
......@@ -10,6 +10,7 @@
#include "settings.h"
#include "motor.h"
#include "brake.h"
class Rotor
{
......@@ -20,10 +21,10 @@ public:
double getAzimuth() const;
double getElevation() const;
void releaseBreakAzimuth();
void releaseBreakElevation();
void tightenBreakAzimuth();
void tightenBreakElevation();
void releaseBrakeAzimuth();
void releaseBrakeElevation();
void tightenBrakeAzimuth();
void tightenBrakeElevation();
void stopAzimuth();
void stopElevation();
......@@ -37,6 +38,7 @@ public:
private:
Motor *azimuth_;
Motor *elevation_;
Brake *brake_;
};
#endif //ROTOR_H
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