summaryrefslogtreecommitdiffstats
path: root/source/Concept/Framework/engine.h
diff options
context:
space:
mode:
Diffstat (limited to 'source/Concept/Framework/engine.h')
-rw-r--r--source/Concept/Framework/engine.h102
1 files changed, 102 insertions, 0 deletions
diff --git a/source/Concept/Framework/engine.h b/source/Concept/Framework/engine.h
index 2736a7a..6454a32 100644
--- a/source/Concept/Framework/engine.h
+++ b/source/Concept/Framework/engine.h
@@ -9,20 +9,105 @@ public:
Engine()
{
this->enabled = false;
+ this->curSpeed = 0;
this->parent = NULL;
+ this->moduleId = 0;
+ this->hardwarePort = NULL;
+ this->pwmSpeed = NULL;
+ this->pwmPort = NULL;
+ this->pinForward = 0;
+ this->pinReverse = 0;
+ this->pinPwm = 0;
}
Engine(uint32 engineId)
{
this->enabled = false;
+ this->curSpeed = 0;
this->parent = NULL;
this->moduleId = engineId;
+ this->pwmSpeed = NULL;
+ this->pwmPort = NULL;
+ this->pinPwm = 0;
+
+ switch(engineId)
+ {
+ case IO_ENGINE_DRIVE_LEFT:
+ this->hardwarePort = &PORTB;
+ this->pwmSpeed = &OCR1A;
+ this->pinForward = (1 << 0);
+ this->pinReverse = (1 << 1);
+ break;
+ case IO_ENGINE_DRIVE_RIGHT:
+ this->hardwarePort = &PORTB;
+ this->pwmSpeed = &OCR1B;
+ this->pinForward = (1 << 2);
+ this->pinReverse = (1 << 3);
+ break;
+ case IO_ENGINE_DRIVE_BACK:
+ this->hardwarePort = &PORTD;
+ this->pwmSpeed = &OCR3A;
+ this->pinForward = (1 << 5);
+ this->pinReverse = (1 << 4);
+ break;
+ case IO_ENGINE_DRIBBLER:
+ this->hardwarePort = &PORTD;
+ this->pwmPort = &PORTA;
+ this->pinForward = (1 << 6);
+ this->pinReverse = (1 << 7);
+ this->pinPwm = (1 << 5);
+ break;
+ default:
+ this->hardwarePort = NULL;
+ this->pwmSpeed = NULL;
+ this->pinForward = 0;
+ this->pinReverse = 0;
+ break;
+ }
+
+ *this->pwmSpeed = 0;
}
protected:
bool enabled;
float curSpeed;
+ //Hardware
+ volatile uint8* hardwarePort;
+ volatile uint16* pwmSpeed;
+ uint8 pinForward;
+ uint8 pinReverse;
+ //Dribbler only
+ volatile uint8* pwmPort;
+ uint8 pinPwm;
+
+ void UpdateDirection()
+ {
+ if(enabled)
+ {
+ if(curSpeed > 0)
+ {
+ *hardwarePort |= pinForward;
+ *hardwarePort &= ~pinReverse;
+ }
+ else if(curSpeed < 0)
+ {
+ *hardwarePort |= pinReverse;
+ *hardwarePort &= ~pinForward;
+ }
+ else
+ {
+ *hardwarePort |= pinForward;
+ *hardwarePort |= pinReverse;
+ }
+ }
+ else
+ {
+ *hardwarePort &= ~pinForward;
+ *hardwarePort &= ~pinReverse;
+ }
+ }
+
public:
float GetSpeed()
{
@@ -32,6 +117,21 @@ public:
void SetSpeed(float newSpeed)
{
curSpeed = newSpeed;
+
+ if(pwmSpeed)
+ {
+ *pwmSpeed = uint16(abs(newSpeed / SPEED_PER_PWM));
+ }
+ else if(pwmPort && uint16(abs(newSpeed / SPEED_PER_PWM)))
+ {
+ *pwmPort |= pinPwm;
+ }
+ else if(pwmPort)
+ {
+ *pwmPort &= ~pinPwn;
+ }
+
+ UpdateDirection();
}
bool GetEnabled()
@@ -42,6 +142,8 @@ public:
void SetEnabled(bool newStatus)
{
enabled = newStatus;
+
+ UpdateDirection();
}
};