summaryrefslogtreecommitdiffstats
path: root/source/Concept/Framework/robot.cpp
blob: f14f2f73c32d81153179c950372a125eec1e483f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "robot.h"

//-----------------------------------------------------------------------------
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
	for(uint8 i = 0; i < IO_END; i++)
	{
		modules[i] = NULL;
	}
}

//-----------------------------------------------------------------------------
Robot::~Robot()
{
	for(uint8 i = 0; i < IO_END; i++)
	{
		if(!modules[i]) continue;

		delete modules[i];
		modules[i] = NULL;
	}
}

//-----------------------------------------------------------------------------
bool Robot::AddModule(IO_Module* newModule)
{
	if(modules[newModule->GetId()])
		return false;

	newModule->SetParent(this);

	modules[newModule->GetId()] = newModule;

	return true;
}

//-----------------------------------------------------------------------------
bool Robot::RemoveModule(IO_Module* oldModule)
{
	return RemoveModule(oldModule->GetId());
}

//-----------------------------------------------------------------------------
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 ---