This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
rc2007-soccer/source/Concept/Framework/modules/output/engine.h

128 lines
2.1 KiB
C
Raw Permalink Normal View History

2007-02-18 00:14:00 +00:00
#ifndef _ENGINE_H
#define _ENGINE_H
#include "../../stdafx.h"
class Engine : public IO_Module
{
public:
Engine()
{
this->enabled = false;
this->curSpeed = 0;
this->parent = NULL;
this->moduleId = 0;
this->hardwarePort = NULL;
this->pwmSpeed = NULL;
this->pinForward = 0;
this->pinReverse = 0;
}
Engine(uint32 engineId)
{
this->enabled = false;
this->curSpeed = 0;
this->parent = NULL;
this->moduleId = engineId;
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_BACK:
this->hardwarePort = &PORTB;
this->pwmSpeed = &OCR1B;
this->pinForward = (1 << 2);
this->pinReverse = (1 << 3);
break;
case IO_ENGINE_DRIVE_RIGHT:
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;
}
protected:
bool enabled;
2007-02-19 22:02:01 +00:00
int16 curSpeed;
2007-02-18 00:14:00 +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;
}
}
public:
2007-02-19 22:02:01 +00:00
int16 GetSpeed()
2007-02-18 00:14:00 +00:00
{
return curSpeed;
}
2007-02-19 22:02:01 +00:00
void SetSpeed(int16 newSpeed)
2007-02-18 00:14:00 +00:00
{
curSpeed = newSpeed;
2007-02-19 22:02:01 +00:00
uint16 pwm = abs(newSpeed);
2007-02-19 20:57:03 +00:00
if(pwm > 255) pwm = 255;
*pwmSpeed = pwm;
2007-02-18 00:14:00 +00:00
UpdateDirection();
}
bool GetEnabled()
{
return enabled;
}
void SetEnabled(bool newStatus)
{
enabled = newStatus;
UpdateDirection();
}
};
#endif