Commit 57358696 authored by markus's avatar markus

Start refactoring rot0r control

parent c87c1289
#include "communicator.h"
Communicator::Communicator()
{
}
//String Communicator::getCommandString(MessageError* msg_error)
//{
// *msg_error = MSG_NONE_AVAILABLE;
// return "";
//}
//Communicator::messageError Communicator::sendMessage(String message)
//{
// Serial.println("whaaaat");
// return Communicator::MSG_NONE_AVAILABLE;
//}
#ifndef COMMUNICATOR_H
#define COMMUNICATOR_H
#include <Arduino.h>
class Communicator
{
public:
enum messageError { MSG_OK,
MSG_NONE_AVAILABLE };
Communicator();
String getCommandString(messageError*);
messageError sendMessage(String);
private:
};
#endif // COMMUNICATOR_H
#include "control.h"
#include <string.h>
Control::Control(Rotor *rotor)
{
rotor_ = rotor;
debugTime_ = 0;
}
void Control::doCommunicate()
{
String command_string;
Communicator::messageError msg_r;
//if((command_string = udp_communicator_.getCommandString(&msg_r)) && (msg_r == Communicator::MSG_OK)) {
// executeCommandString(command_string, &udp_communicator_);
//}
if((command_string = serial_communicator_->getCommandString(&msg_r)) && (msg_r == Communicator::MSG_OK)) {
if(command_string != "") {
Serial.println(command_string);
}
executeCommandString(&command_string, serial_communicator_);
}
}
uint16 Control::setElevation(uint16 el)
{
return rotor_->setElevation(el);
}
uint16 Control::setAzimuth(uint16 az)
{
return rotor_->setAzimuth(az);
}
void Control::doControl()
{
rotor_->doRotor();
doCommunicate();
if(debugTime_ > 100)
{
//hamlib.debugOut();
rotor_->debugOut();
debugTime_ = 0;
}
debugTime_++;
}
Control::hamlibError Control::executeCommandString(String *command_string, Communicator *communicator)
{
String substring;
int lastPos = -1;
int newPos = 0;
int dst = 0;
while (newPos = command_string->indexOf(";", lastPos + 1)) {
substring = command_string->substring(lastPos + 1, newPos);
lastPos = newPos;
if ( substring == "state" )
{
// GNAAAARRR
//communicator->sendMessage(String(String(AZIM_MIN) + "/" + String(AZIM_MAX) + " " + String(ELEV_MIN) + "/" + String(ELEV_MAX)));
}
else if ( substring == "get position" ) {
// GNAAAARRR
//communicator->sendMessage(String(rotor_->getActualAzimuth()) + " " + String(rotor_->getActualElevation()));
}
else if ( substring.startsWith("set az") ) {
if( substring.length() < 8 )
return HAMLIB_NOK;
dst = substring.substring(7).toInt();
if ( dst == 0 && substring.substring(7) != "0" )
return HAMLIB_NOK;
if ( dst < AZIM_MIN or dst > AZIM_MAX )
return HAMLIB_NOK;
rotor_->setAzimuth(dst);
}
else if ( substring.startsWith("set el") ) {
if( substring.length() < 8 )
return HAMLIB_NOK;
dst = substring.substring(7).toInt();
if ( dst == 0 && substring.substring(7) != "0" )
return HAMLIB_NOK;
if ( dst < ELEV_MIN or dst > ELEV_MAX )
return HAMLIB_NOK;
rotor_->setElevation(dst);
}
else
return HAMLIB_NOK;
}
return HAMLIB_OK;
}
#ifndef CONTROL_H
#define CONTROL_H
#include "Arduino.h"
#include "rotor.h"
#include "serial_communicator.h"
#include "udp_communicator.h"
class Control
{
public:
enum hamlibError{HAMLIB_OK,
HAMLIB_NOK };
Control(Rotor *);
void doControl();
uint16 setAzimuth(uint16);
uint16 setElevation(uint16);
//uint16 getAz() const;
//uint16 getEl() const;
private:
void doCommunicate();
hamlibError executeCommandString(String*, Communicator*);
Rotor *rotor_;
SerialCommunicator *serial_communicator_;
UDPCommunicator *udp_communicator_;
int debugTime_;
};
#endif // CONTROL_H
......@@ -29,8 +29,8 @@ void Rotor::initRotor()
pinMode(AZIM_RE_CCW, OUTPUT);
pinMode(AZIM_RE_BREAK, OUTPUT);
//All Relais Off
digitalWrite(AZIM_RE_CW, HIGH);
digitalWrite(AZIM_RE_CCW, HIGH);
digitalWrite(AZIM_RE_CW, HIGH);
digitalWrite(AZIM_RE_CCW, HIGH);
digitalWrite(AZIM_RE_BREAK, HIGH);
//Initial Values
m_bAzimuthBreakReleased = 0;
......@@ -289,7 +289,7 @@ Rotor::rotorError Rotor::readRotSensors()
m_uActualAzimuth = (uint16)(readADC(AZIM_POT)/AZIM_DEG);
if(ELEV)
m_uActualElevation = (uint16)(readADC(ELEV_POT)/ELEV_DEG);
return Rotor::ROT_OK;
}
......
......@@ -14,8 +14,8 @@
class Rotor
{
public:
enum rotorError { ROT_OK,
ROT_NOK,
enum rotorError { ROT_OK,
ROT_NOK,
ROT_VAL_OUT_OF_RANGE,
ROT_BREAK_NOT_RELEASED };
Rotor();
......
#include "serial_communicator.h"
SerialCommunicator::SerialCommunicator()
{
Serial.setTimeout(100);
}
String SerialCommunicator::getCommandString(messageError *err)
{
String result;
if (Serial.available() > 0) {
result = Serial.readString();
*err = MSG_OK;
return result;
}
*err = MSG_NONE_AVAILABLE;
return "";
}
SerialCommunicator::messageError SerialCommunicator::sendMessage(String message)
{
Serial.println("tst");
Serial.println(message);
return Communicator::MSG_OK;
}
#ifndef SERIAL_COMMUNICATOR_H
#define SERIAL_COMMUNICATOR_H
#include "communicator.h"
#include "Arduino.h"
class SerialCommunicator: public Communicator
{
public:
SerialCommunicator();
String getCommandString(messageError*);
messageError sendMessage(String);
private:
};
#endif // SERIAL_COMMUNICATOR_H
......@@ -11,6 +11,7 @@
#include <SPI.h>
#include <Ethernet.h>
#include "control.h"
#include "rotor.h"
#include "hamlib.h"
......@@ -23,6 +24,7 @@ unsigned int localPort = 51337;
Rotor rotor;
CHamlib hamlib;
EthernetUDP udp;
Control control(&rotor);
int debugTime;
void setup()
......@@ -30,6 +32,7 @@ void setup()
// Init
rotor.initRotor();
// Open serial communications and wait for port to open:
Serial.begin(19200);
while (!Serial) {
......@@ -45,21 +48,17 @@ void setup()
//Debug
debugTime = 0;
rotor.setElevation(170);
rotor.setAzimuth(42);
//rotor.releaseBreakAzimuth();
//rotor.releaseBreakElevation();
//rotor.rotateUp();
control.setElevation(23);
control.setAzimuth(42);
}
void loop()
{
//hamlib.receiveHamlibPacket();
rotor.doRotor();
control.doControl();
//hamlib.sendHamlibPacket();
if(debugTime > 100)
{
//hamlib.debugOut();
rotor.debugOut();
debugTime = 0;
}
debugTime++;
}
#include "udp_communicator.h"
#ifndef UDP_COMMUNICATOR_H
#define UDP_COMMUNICATOR_H
#include "communicator.h"
#include <Arduino.h>
class UDPCommunicator: public Communicator
{
public:
private:
};
#endif // UDP_COMMUNICATOR_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