From 91b2508dc92a97802353bd2e96f2849c835f0b2a Mon Sep 17 00:00:00 2001 From: masterm Date: Thu, 15 Feb 2007 20:33:05 +0000 Subject: +++ many updates to sources --- source/Concept/Framework/Concept.vcproj | 72 +++++++++++++++++++ source/Concept/Framework/defines.h | 38 +++++++--- source/Concept/Framework/distance_sensor.cpp | 1 + source/Concept/Framework/distance_sensor.h | 27 +++++++ source/Concept/Framework/engine.h | 102 +++++++++++++++++++++++++++ source/Concept/Framework/io_module.h | 1 + source/Concept/Framework/ir_sensor.cpp | 9 +++ source/Concept/Framework/ir_sensor.h | 62 ++++++++++++++++ source/Concept/Framework/kicker.cpp | 1 + source/Concept/Framework/kicker.h | 84 ++++++++++++++++++++++ source/Concept/Framework/led.cpp | 1 + source/Concept/Framework/led.h | 65 +++++++++++++++++ source/Concept/Framework/main.cpp | 45 +++++++++--- source/Concept/Framework/robot.cpp | 80 +++++++++++++++++++++ source/Concept/Framework/robot.h | 2 + source/Concept/Framework/sensor.h | 12 ++-- source/Concept/Framework/stdafx.h | 4 ++ 17 files changed, 579 insertions(+), 27 deletions(-) create mode 100644 source/Concept/Framework/distance_sensor.cpp create mode 100644 source/Concept/Framework/distance_sensor.h create mode 100644 source/Concept/Framework/ir_sensor.cpp create mode 100644 source/Concept/Framework/ir_sensor.h create mode 100644 source/Concept/Framework/kicker.cpp create mode 100644 source/Concept/Framework/kicker.h create mode 100644 source/Concept/Framework/led.cpp create mode 100644 source/Concept/Framework/led.h diff --git a/source/Concept/Framework/Concept.vcproj b/source/Concept/Framework/Concept.vcproj index fd22f8d..1348191 100644 --- a/source/Concept/Framework/Concept.vcproj +++ b/source/Concept/Framework/Concept.vcproj @@ -201,6 +201,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + parent = NULL; + this->moduleId = 0; + } + + Distance_Sensor(uint32 sensorId) + { + this->parent = NULL; + this->moduleId = sensorId; + } + +protected: + +public: +}; + +#endif 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(); } }; diff --git a/source/Concept/Framework/io_module.h b/source/Concept/Framework/io_module.h index af5d611..f1fecd3 100644 --- a/source/Concept/Framework/io_module.h +++ b/source/Concept/Framework/io_module.h @@ -11,6 +11,7 @@ public: IO_Module() { this->parent = NULL; + this->moduleId = 0; } IO_Module(uint32 moduleId) diff --git a/source/Concept/Framework/ir_sensor.cpp b/source/Concept/Framework/ir_sensor.cpp new file mode 100644 index 0000000..065143e --- /dev/null +++ b/source/Concept/Framework/ir_sensor.cpp @@ -0,0 +1,9 @@ +#include "ir_sensor.h" + +//----------------------------------------------------------------------------- +uint16 IR_Sensor::GetIRIntensity() +{ + if(!parent) return 0; + + return parent->GetADCValue(channel); +} \ No newline at end of file diff --git a/source/Concept/Framework/ir_sensor.h b/source/Concept/Framework/ir_sensor.h new file mode 100644 index 0000000..f267644 --- /dev/null +++ b/source/Concept/Framework/ir_sensor.h @@ -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 diff --git a/source/Concept/Framework/kicker.cpp b/source/Concept/Framework/kicker.cpp new file mode 100644 index 0000000..94bd711 --- /dev/null +++ b/source/Concept/Framework/kicker.cpp @@ -0,0 +1 @@ +#include "kicker.h" \ No newline at end of file diff --git a/source/Concept/Framework/kicker.h b/source/Concept/Framework/kicker.h new file mode 100644 index 0000000..fa9718e --- /dev/null +++ b/source/Concept/Framework/kicker.h @@ -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 diff --git a/source/Concept/Framework/led.cpp b/source/Concept/Framework/led.cpp new file mode 100644 index 0000000..a81aaf4 --- /dev/null +++ b/source/Concept/Framework/led.cpp @@ -0,0 +1 @@ +#include "led.h" \ No newline at end of file diff --git a/source/Concept/Framework/led.h b/source/Concept/Framework/led.h new file mode 100644 index 0000000..f0f3982 --- /dev/null +++ b/source/Concept/Framework/led.h @@ -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 diff --git a/source/Concept/Framework/main.cpp b/source/Concept/Framework/main.cpp index cf80770..d6e1d92 100644 --- a/source/Concept/Framework/main.cpp +++ b/source/Concept/Framework/main.cpp @@ -13,26 +13,51 @@ int main() 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 for(uint8 i = IO_SENSOR_START; i < IO_SENSOR_END; i++) { - SensorTypes newSensorType; - switch(i) { - //Cases when sensor is digital: - // newSensorType = SENSOR_TYPE_DIGITAL; - // break; - + //Create correct type of sensor + case IO_SENSOR_IR_0_DEG: + case IO_SENSOR_IR_30_DEG: + case IO_SENSOR_IR_60_DEG: + case IO_SENSOR_IR_100_DEG: + case IO_SENSOR_IR_180_DEG: + case IO_SENSOR_IR_260_DEG: + case IO_SENSOR_IR_300_DEG: + case IO_SENSOR_IR_330_DEG: + { + IR_Sensor* newSensor = new IR_Sensor(i); + localRobot->AddModule(newSensor); + newSensor = NULL; + break; + } //Other cases default: - newSensorType = SENSOR_TYPE_ANALOG; + { + Sensor* newSensor = new Sensor(i); + localRobot->AddModule(newSensor); + newSensor = NULL; break; + } } + } - Sensor* newSensor = new Sensor(i, newSensorType); - localRobot->AddModule(newSensor); - newSensor = NULL; + //Init Leds + for(uint8 i = IO_LED_START; i < IO_LED_END; i++) + { + Led* newLed = new Led(i); + localRobot->AddModule(newLed); + newLed = NULL; } //Run diff --git a/source/Concept/Framework/robot.cpp b/source/Concept/Framework/robot.cpp index f982be6..3d3ae48 100644 --- a/source/Concept/Framework/robot.cpp +++ b/source/Concept/Framework/robot.cpp @@ -3,6 +3,48 @@ //----------------------------------------------------------------------------- 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 < enable counter + + // activate Kanal A+B on PWM3 at 8Bit + TCCR3A = (1 << COM3A1) | (1 << COM3B1) | (1 << WGM10); + TCCR3B = (1 < enable counter + + //Activate interrupt + sei(); + + //Interface memset(modules, NULL, sizeof(modules)); } @@ -43,4 +85,42 @@ void Robot::Update() //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 --- diff --git a/source/Concept/Framework/robot.h b/source/Concept/Framework/robot.h index db7fe15..13aa696 100644 --- a/source/Concept/Framework/robot.h +++ b/source/Concept/Framework/robot.h @@ -38,6 +38,8 @@ public: bool RemoveModule(IO_Module* oldModule); void Update(); + + uint16 GetADCValue(uint8 channel); }; #endif diff --git a/source/Concept/Framework/sensor.h b/source/Concept/Framework/sensor.h index 117f446..62f2165 100644 --- a/source/Concept/Framework/sensor.h +++ b/source/Concept/Framework/sensor.h @@ -1,7 +1,8 @@ #ifndef _SENSOR_H #define _SENSOR_H -#include "stdafx.h" +#include "defines.h" +#include "io_module.h" class Sensor : public IO_Module { @@ -9,23 +10,18 @@ public: Sensor() { this->parent = NULL; + this->moduleId = 0; } - Sensor(uint32 sensorId, SensorTypes sensorType) + Sensor(uint32 sensorId) { this->parent = NULL; this->moduleId = sensorId; - this->sensorType = sensorType; } protected: - SensorTypes sensorType; public: - SensorTypes GetType() - { - return sensorType; - } }; #endif diff --git a/source/Concept/Framework/stdafx.h b/source/Concept/Framework/stdafx.h index 5a50add..436f117 100644 --- a/source/Concept/Framework/stdafx.h +++ b/source/Concept/Framework/stdafx.h @@ -9,4 +9,8 @@ #include "io_module.h" #include "sensor.h" #include "engine.h" +#include "kicker.h" +#include "led.h" +#include "distance_sensor.h" +#include "ir_sensor.h" #include "robot.h" -- cgit v1.2.3