2007-01-14 18:07:03 +00:00
|
|
|
#ifndef _ENGINE_H
|
|
|
|
#define _ENGINE_H
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
|
|
|
class Engine : public IO_Module
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Engine()
|
|
|
|
{
|
2007-02-13 14:40:04 +00:00
|
|
|
this->enabled = false;
|
2007-02-15 20:33:05 +00:00
|
|
|
this->curSpeed = 0;
|
2007-02-13 14:40:04 +00:00
|
|
|
this->parent = NULL;
|
2007-02-15 20:33:05 +00:00
|
|
|
this->moduleId = 0;
|
|
|
|
this->hardwarePort = NULL;
|
|
|
|
this->pwmSpeed = NULL;
|
|
|
|
this->pinForward = 0;
|
|
|
|
this->pinReverse = 0;
|
2007-01-14 18:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Engine(uint32 engineId)
|
|
|
|
{
|
2007-02-13 14:40:04 +00:00
|
|
|
this->enabled = false;
|
2007-02-15 20:33:05 +00:00
|
|
|
this->curSpeed = 0;
|
2007-02-13 14:40:04 +00:00
|
|
|
this->parent = NULL;
|
2007-01-14 18:07:03 +00:00
|
|
|
this->moduleId = engineId;
|
2007-02-15 20:33:05 +00:00
|
|
|
|
|
|
|
switch(engineId)
|
|
|
|
{
|
|
|
|
case IO_ENGINE_DRIVE_LEFT:
|
|
|
|
this->hardwarePort = &PORTB;
|
|
|
|
this->pwmSpeed = &OCR1A;
|
|
|
|
this->pinForward = (1 << 0);
|
|
|
|
this->pinReverse = (1 << 1);
|
|
|
|
break;
|
2007-02-17 00:35:01 +00:00
|
|
|
case IO_ENGINE_DRIVE_BACK:
|
2007-02-15 20:33:05 +00:00
|
|
|
this->hardwarePort = &PORTB;
|
|
|
|
this->pwmSpeed = &OCR1B;
|
|
|
|
this->pinForward = (1 << 2);
|
|
|
|
this->pinReverse = (1 << 3);
|
|
|
|
break;
|
2007-02-17 00:35:01 +00:00
|
|
|
case IO_ENGINE_DRIVE_RIGHT:
|
2007-02-15 20:33:05 +00:00
|
|
|
this->hardwarePort = &PORTD;
|
|
|
|
this->pwmSpeed = &OCR3A;
|
|
|
|
this->pinForward = (1 << 5);
|
|
|
|
this->pinReverse = (1 << 4);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this->hardwarePort = NULL;
|
|
|
|
this->pwmSpeed = NULL;
|
|
|
|
this->pinForward = 0;
|
|
|
|
this->pinReverse = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*this->pwmSpeed = 0;
|
2007-01-14 18:07:03 +00:00
|
|
|
}
|
2007-02-13 14:40:04 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool enabled;
|
|
|
|
float curSpeed;
|
|
|
|
|
2007-02-15 20:33:05 +00:00
|
|
|
//Hardware
|
|
|
|
volatile uint8* hardwarePort;
|
|
|
|
volatile uint16* pwmSpeed;
|
|
|
|
uint8 pinForward;
|
|
|
|
uint8 pinReverse;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-13 14:40:04 +00:00
|
|
|
public:
|
|
|
|
float GetSpeed()
|
|
|
|
{
|
|
|
|
return curSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSpeed(float newSpeed)
|
|
|
|
{
|
|
|
|
curSpeed = newSpeed;
|
2007-02-15 20:33:05 +00:00
|
|
|
|
2007-02-17 13:42:00 +00:00
|
|
|
*pwmSpeed = (abs((int16)(newSpeed / SPEED_PER_PWM)));
|
2007-02-15 20:33:05 +00:00
|
|
|
|
|
|
|
UpdateDirection();
|
2007-02-13 14:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetEnabled()
|
|
|
|
{
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetEnabled(bool newStatus)
|
|
|
|
{
|
|
|
|
enabled = newStatus;
|
2007-02-15 20:33:05 +00:00
|
|
|
|
|
|
|
UpdateDirection();
|
2007-02-13 14:40:04 +00:00
|
|
|
}
|
2007-01-14 18:07:03 +00:00
|
|
|
};
|
|
|
|
|
2007-02-04 21:38:03 +00:00
|
|
|
#endif
|