+++ many updates to sources

This commit is contained in:
masterm 2007-02-15 20:33:05 +00:00
parent 8d7053ca84
commit 91b2508dc9
17 changed files with 579 additions and 27 deletions

View file

@ -201,6 +201,78 @@
</File> </File>
</Filter> </Filter>
</Filter> </Filter>
<Filter
Name="Led"
Filter="">
<Filter
Name="Source Files"
Filter="">
<File
RelativePath=".\led.cpp">
</File>
</Filter>
<Filter
Name="Header Files"
Filter="">
<File
RelativePath=".\led.h">
</File>
</Filter>
</Filter>
<Filter
Name="Distance_Sensor"
Filter="">
<Filter
Name="Source Files"
Filter="">
<File
RelativePath=".\distance_sensor.cpp">
</File>
</Filter>
<Filter
Name="Header Files"
Filter="">
<File
RelativePath=".\distance_sensor.h">
</File>
</Filter>
</Filter>
<Filter
Name="IR_Sensor"
Filter="">
<Filter
Name="Source Files"
Filter="">
<File
RelativePath=".\ir_sensor.cpp">
</File>
</Filter>
<Filter
Name="Header Files"
Filter="">
<File
RelativePath=".\ir_sensor.h">
</File>
</Filter>
</Filter>
<Filter
Name="Kicker"
Filter="">
<Filter
Name="Source Files"
Filter="">
<File
RelativePath=".\kicker.cpp">
</File>
</Filter>
<Filter
Name="Header Files"
Filter="">
<File
RelativePath=".\kicker.h">
</File>
</Filter>
</Filter>
</Filter> </Filter>
<Filter <Filter
Name="Hardware Interface" Name="Hardware Interface"

View file

@ -55,12 +55,8 @@
#define uint64 unsigned int64 #define uint64 unsigned int64
#endif #endif
//Sensor types //Constants
enum SensorTypes #define SPEED_PER_PWM 1
{
SENSOR_TYPE_ANALOG,
SENSOR_TYPE_DIGITAL,
};
//IO Module Names //IO Module Names
enum IOModuleNames enum IOModuleNames
@ -74,18 +70,42 @@ enum IOModuleNames
IO_ENGINE_DRIVE_LEFT = IO_ENGINE_START, IO_ENGINE_DRIVE_LEFT = IO_ENGINE_START,
IO_ENGINE_DRIVE_RIGHT, IO_ENGINE_DRIVE_RIGHT,
IO_ENGINE_DRIVE_BACK, IO_ENGINE_DRIVE_BACK,
IO_ENGINE_DRIBBLER,
IO_ENGINE_END, IO_ENGINE_END,
//Sensors //Kicker
IO_SENSOR_START = IO_ENGINE_END,
IO_SENSOR_MOUSE = IO_SENSOR_START, IO_KICKER_START = IO_ENGINE_END,
IO_KICKER_MAIN = IO_KICKER_START,
IO_KICKER_END,
//Sensors
IO_SENSOR_START = IO_KICKER_END,
IO_SENSOR_IR_0_DEG = IO_SENSOR_START,
IO_SENSOR_IR_30_DEG,
IO_SENSOR_IR_60_DEG,
IO_SENSOR_IR_100_DEG,
IO_SENSOR_IR_180_DEG,
IO_SENSOR_IR_260_DEG,
IO_SENSOR_IR_300_DEG,
IO_SENSOR_IR_330_DEG,
IO_SENSOR_END, IO_SENSOR_END,
//Leds
IO_LED_START = IO_SENSOR_END,
IO_LED_MAIN = IO_LED_START,
IO_LED_END,
//General //General
IO_END = IO_SENSOR_END, IO_END = IO_LED_END,
}; };
#endif #endif

View file

@ -0,0 +1 @@
#include "distance_sensor.h"

View file

@ -0,0 +1,27 @@
#ifndef _DISTANCE_SENSOR_H
#define _DISTANCE_SENSOR_H
//#include "stdafx.h"
#include "sensor.h"
class Distance_Sensor : public Sensor
{
public:
Distance_Sensor()
{
this->parent = NULL;
this->moduleId = 0;
}
Distance_Sensor(uint32 sensorId)
{
this->parent = NULL;
this->moduleId = sensorId;
}
protected:
public:
};
#endif

View file

@ -9,20 +9,105 @@ public:
Engine() Engine()
{ {
this->enabled = false; this->enabled = false;
this->curSpeed = 0;
this->parent = NULL; 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) Engine(uint32 engineId)
{ {
this->enabled = false; this->enabled = false;
this->curSpeed = 0;
this->parent = NULL; this->parent = NULL;
this->moduleId = engineId; 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: protected:
bool enabled; bool enabled;
float curSpeed; 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: public:
float GetSpeed() float GetSpeed()
{ {
@ -32,6 +117,21 @@ public:
void SetSpeed(float newSpeed) void SetSpeed(float newSpeed)
{ {
curSpeed = 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() bool GetEnabled()
@ -42,6 +142,8 @@ public:
void SetEnabled(bool newStatus) void SetEnabled(bool newStatus)
{ {
enabled = newStatus; enabled = newStatus;
UpdateDirection();
} }
}; };

View file

@ -11,6 +11,7 @@ public:
IO_Module() IO_Module()
{ {
this->parent = NULL; this->parent = NULL;
this->moduleId = 0;
} }
IO_Module(uint32 moduleId) IO_Module(uint32 moduleId)

View file

@ -0,0 +1,9 @@
#include "ir_sensor.h"
//-----------------------------------------------------------------------------
uint16 IR_Sensor::GetIRIntensity()
{
if(!parent) return 0;
return parent->GetADCValue(channel);
}

View file

@ -0,0 +1,62 @@
#ifndef _IR_SENSOR_H
#define _IR_SENSOR_H
#include "defines.h"
#include "robot.h"
#include "sensor.h"
class IR_Sensor : public Sensor
{
public:
IR_Sensor()
{
this->parent = NULL;
this->moduleId = 0;
}
IR_Sensor(uint32 sensorId)
{
this->parent = NULL;
this->moduleId = sensorId;
switch(sensorId)
{
case IO_SENSOR_IR_0_DEG:
this->channel = 0;
break;
case IO_SENSOR_IR_30_DEG:
this->channel = 1;
break;
case IO_SENSOR_IR_60_DEG:
this->channel = 2;
break;
case IO_SENSOR_IR_100_DEG:
this->channel = 3;
break;
case IO_SENSOR_IR_180_DEG:
this->channel = 4;
break;
case IO_SENSOR_IR_260_DEG:
this->channel = 5;
break;
case IO_SENSOR_IR_300_DEG:
this->channel = 6;
break;
case IO_SENSOR_IR_330_DEG:
this->channel = 7;
break;
default:
this->channel = 8;
break;
}
}
protected:
//Hardware
uint8 channel;
public:
uint16 GetIRIntensity();
};
#endif

View file

@ -0,0 +1 @@
#include "kicker.h"

View file

@ -0,0 +1,84 @@
#ifndef _KICKER_H
#define _KICKER_H
#include "stdafx.h"
class Kicker : public IO_Module
{
public:
Kicker()
{
this->enabled = false;
this->parent = NULL;
this->moduleId = 0;
this->portPower = NULL;
this->portForward = NULL;
this->portReverse = NULL;
this->pinPower = 0;
this->pinForward = 0;
this->pinReverse = 0;
}
Kicker(uint32 kickerId)
{
this->enabled = false;
this->parent = NULL;
this->moduleId = kickerId;
switch(kickerId)
{
case IO_KICKER_MAIN:
this->portPower = &PORTG;
this->portForward = &PORTA;
this->portReverse = &PORTE;
this->pinPower = (1 << 3);
this->pinForward = (1 << 2);
this->pinReverse = (1 << 6);
break;
default:
this->portPower = NULL;
this->portForward = NULL;
this->portReverse = NULL;
this->pinPower = 0;
this->pinForward = 0;
this->pinReverse = 0;
break;
}
*this->portForward |= this->pinForward;
*this->portReverse &= ~this->pinReverse;
}
protected:
bool enabled;
//Hardware
volatile uint8* portPower;
volatile uint8* portForward;
volatile uint8* portReverse;
uint8 pinPower;
uint8 pinForward;
uint8 pinReverse;
public:
bool GetEnabled()
{
return enabled;
}
void SetEnabled(bool newStatus)
{
enabled = newStatus;
if(enabled)
{
*portPower |= pinPower;
}
else
{
*portPower &= ~pinPower;
}
}
};
#endif

View file

@ -0,0 +1 @@
#include "led.h"

View file

@ -0,0 +1,65 @@
#ifndef _LED_H
#define _LED_H
#include "stdafx.h"
class Led : public IO_Module
{
public:
Led()
{
this->enabled = false;
this->parent = NULL;
this->moduleId = 0;
this->hardwarePort = NULL;
this->pinPower = 0;
}
Led(uint32 ledId)
{
this->enabled = false;
this->parent = NULL;
this->moduleId = ledId;
switch(ledId)
{
case IO_LED_MAIN:
this->hardwarePort = &PORTB;
this->pinPower = (1 << 1);
break;
default:
this->hardwarePort = NULL;
this->pinPower = 0;
break;
}
}
protected:
bool enabled;
//Hardware
volatile uint8* hardwarePort;
uint8 pinPower;
public:
bool GetEnabled()
{
return enabled;
}
void SetEnabled(bool newStatus)
{
enabled = newStatus;
if(enabled)
{
*hardwarePort &= ~pinPower;
}
else
{
*hardwarePort |= pinPower;
}
}
};
#endif

View file

@ -13,26 +13,51 @@ int main()
newEngine = NULL; newEngine = NULL;
} }
//Init Kicker
for(uint8 i = IO_KICKER_START; i < IO_KICKER_END; i++)
{
Kicker* newKicker = new Kicker(i);
localRobot->AddModule(newKicker);
newKicker = NULL;
}
//Init Sensors //Init Sensors
for(uint8 i = IO_SENSOR_START; i < IO_SENSOR_END; i++) for(uint8 i = IO_SENSOR_START; i < IO_SENSOR_END; i++)
{ {
SensorTypes newSensorType;
switch(i) switch(i)
{ {
//Cases when sensor is digital: //Create correct type of sensor
// newSensorType = SENSOR_TYPE_DIGITAL; case IO_SENSOR_IR_0_DEG:
// break; case IO_SENSOR_IR_30_DEG:
case IO_SENSOR_IR_60_DEG:
//Other cases case IO_SENSOR_IR_100_DEG:
default: case IO_SENSOR_IR_180_DEG:
newSensorType = SENSOR_TYPE_ANALOG; case IO_SENSOR_IR_260_DEG:
break; case IO_SENSOR_IR_300_DEG:
} case IO_SENSOR_IR_330_DEG:
{
Sensor* newSensor = new Sensor(i, newSensorType); IR_Sensor* newSensor = new IR_Sensor(i);
localRobot->AddModule(newSensor); localRobot->AddModule(newSensor);
newSensor = NULL; newSensor = NULL;
break;
}
//Other cases
default:
{
Sensor* newSensor = new Sensor(i);
localRobot->AddModule(newSensor);
newSensor = NULL;
break;
}
}
}
//Init Leds
for(uint8 i = IO_LED_START; i < IO_LED_END; i++)
{
Led* newLed = new Led(i);
localRobot->AddModule(newLed);
newLed = NULL;
} }
//Run //Run

View file

@ -3,6 +3,48 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
Robot::Robot() Robot::Robot()
{ {
//Hardware
//Set pin 1-6 output, 0 and 7 input
DDRA = (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6);
PORTA = 0;
//All output
DDRB = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7);
PORTB = (1 << 1);
//All output except PC4/PC5 (mousesensor SDA)
DDRC = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
PORTC = 0;
//All output except PD0+1(I2C) + 2+3(RS232)
DDRD = (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7);
PORTD = (1 << 0) | (1 << 1);//Activate pullup at PD0+1
//PE5 for input
DDRE = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 6) | (1 << 7);
PORTE = 0;
//All input with pullup
DDRF = 0;
PORTF = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7);
//All input
DDRG = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
PORTG = (1 << 0) | (1 << 1);
// activate channel A+B on PWM1 at 8Bit
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
TCCR1B = (1 <<ICNC1) | (1 <<CS12) | (1 <<CS10); // set clock/prescaler 1/1024 -> enable counter
// activate Kanal A+B on PWM3 at 8Bit
TCCR3A = (1 << COM3A1) | (1 << COM3B1) | (1 << WGM10);
TCCR3B = (1 <<ICNC3) | (1 <<CS32) | (1 <<CS30); // set clock/prescaler 1/1024 -> enable counter
//Activate interrupt
sei();
//Interface
memset(modules, NULL, sizeof(modules)); memset(modules, NULL, sizeof(modules));
} }
@ -43,4 +85,42 @@ void Robot::Update()
//insert code here //insert code here
} }
//-----------------------------------------------------------------------------
uint16 Robot::GetADCValue(uint8 channel)
{
if(channel > 7) return 0;
uint32 result = 0;
//Activate ADC and set division factor to 64
ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1);
//Set multiplexer channel
ADMUX = channel;
//Use internal referencevoltage (2,56 V for atmega32)
ADMUX |= (1 << REFS1) | (1 << REFS0);
//Initialise ADC and start dummyreadout
ADCSRA |= (1 << ADSC);
while(ADCSRA & (1 << ADSC));
//Get voltage three times and calculate average value
for(uint8 i = 0; i < 3; i++)
{
// Eine Wandlung
ADCSRA |= (1 << ADSC);
// Auf Ergebnis warten...
while(ADCSRA & (1 << ADSC));
result += ADCW;
}
//Disable ADC
ADCSRA &= ~(1 << ADEN);
result /= 3;
return uint16(result);
}
//--- EOF --- //--- EOF ---

View file

@ -38,6 +38,8 @@ public:
bool RemoveModule(IO_Module* oldModule); bool RemoveModule(IO_Module* oldModule);
void Update(); void Update();
uint16 GetADCValue(uint8 channel);
}; };
#endif #endif

View file

@ -1,7 +1,8 @@
#ifndef _SENSOR_H #ifndef _SENSOR_H
#define _SENSOR_H #define _SENSOR_H
#include "stdafx.h" #include "defines.h"
#include "io_module.h"
class Sensor : public IO_Module class Sensor : public IO_Module
{ {
@ -9,23 +10,18 @@ public:
Sensor() Sensor()
{ {
this->parent = NULL; this->parent = NULL;
this->moduleId = 0;
} }
Sensor(uint32 sensorId, SensorTypes sensorType) Sensor(uint32 sensorId)
{ {
this->parent = NULL; this->parent = NULL;
this->moduleId = sensorId; this->moduleId = sensorId;
this->sensorType = sensorType;
} }
protected: protected:
SensorTypes sensorType;
public: public:
SensorTypes GetType()
{
return sensorType;
}
}; };
#endif #endif

View file

@ -9,4 +9,8 @@
#include "io_module.h" #include "io_module.h"
#include "sensor.h" #include "sensor.h"
#include "engine.h" #include "engine.h"
#include "kicker.h"
#include "led.h"
#include "distance_sensor.h"
#include "ir_sensor.h"
#include "robot.h" #include "robot.h"