-
Notifications
You must be signed in to change notification settings - Fork 0
/
Seriell.cpp
138 lines (113 loc) · 3.24 KB
/
Seriell.cpp
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
130
131
132
133
134
135
136
137
138
#include "Seriell.hpp"
int CSeriell::InitSeriell () {
m_nOwnSeriellAdress = serialOpen ("/dev/ttyAMA0", 38400); //initalisierung der rs232 Schnittstelle
if (m_nOwnSeriellAdress == -1) {
g_pTracer->Trace (ERROR, "Failed to initialise the seriell bus!");
return -1;
}else{
g_pTracer->Trace (NOTE, "Seriell bus initialised.");
return 1;
}
}
//Diese Funktion wartet 200 mSec auf verfügbare Seriell Daten
int CSeriell::DataAvailableNoTimeOut () {
long nTimeStamp = g_pTimer->TimeSinceStart();
while (serialDataAvail (m_nOwnSeriellAdress) < 1) {
if (nTimeStamp+200 < g_pTimer->TimeSinceStart()) {
g_pTracer->Trace (ERROR, "Communication error: timeout.");
return -1;
}
}
return 1;
}
int CSeriell::GetPhotoSensorData (const int nPhotoSensor) {
if ((nPhotoSensor < 1)||(nPhotoSensor > 4)) {
g_pTracer->Trace (WARNING, "Wrong number for the photo sensor.");
return -1;
}
SendSeriellInt (nPhotoSensor);
if (DataAvailableNoTimeOut () == -1) {
return -1;
}
if (ReceiveSeriellData () == nPhotoSensor) {
return ReceiveSeriellData ();
}else{
g_pTracer->Trace (ERROR, "Error while communicating with the ATmega32.");
return -1;
}
}
float CSeriell::GetBatteryVoltage () {
float fData = 0;
int nSeriellData[2];
SendSeriellInt (5);
if (DataAvailableNoTimeOut () == -1) {
return -1;
}
if (ReceiveSeriellData () == 5) {
nSeriellData[0] = ReceiveSeriellData ();
nSeriellData[1] = ReceiveSeriellData ();
fData = (nSeriellData[1] << 8 ) | nSeriellData[0];
if (fData == 255) {
g_pTracer->Trace (ERROR, "ATmega32 failed to measure the battary voltage.");
return -1;
}else{
return (fData*0.028); //Batteriespannung berechnen
}
}else{
g_pTracer->Trace (ERROR, "Error while communicating with the ATmega32.");
return -1;
}
}
int CSeriell::SetMotorPower (const int nMotor, const int nPower) {
int nSeriellData[2];
nSeriellData[0] = nPower;
nSeriellData[1] = (nPower >> 8);
if ((nMotor < 1)||(nMotor > 4)) {
g_pTracer->Trace (WARNING, "Wrong number for the motor.");
return -1;
}
if ((nPower < -255)||(nPower > 255)) {
g_pTracer->Trace (WARNING, "Wrong number for the motor power.");
return -1;
}
SendSeriellInt (nMotor+20);
if (DataAvailableNoTimeOut () == -1) {
return -1;
}
if (ReceiveSeriellData () == (nMotor+20)) {
SendSeriellInt (nSeriellData[0]);
SendSeriellInt (nSeriellData[1]);
}else{
g_pTracer->Trace (ERROR, "Error while communicating with the ATmega32.");
return -1;
}
return 1;
}
int CSeriell::MovePML (const int nDirection) { //1 Dreht das PML in Fahrtrichtung links
if ((nDirection < 0)||(nDirection > 1)) {
g_pTracer->Trace (WARNING, "Wrong number for the motor direction.");
return -1;
}
SendSeriellInt (20);
if (DataAvailableNoTimeOut () == -1) {
return -1;
}
if (ReceiveSeriellData () == 20) {
SendSeriellInt (nDirection);
}else{
g_pTracer->Trace (ERROR, "Error while communicating with the ATmega32.");
return -1;
}
return 1;
}
int CSeriell::SendSeriellInt (int nData) {
if ((nData < 256)&&(nData > -1)) {
serialPutchar (m_nOwnSeriellAdress, static_cast<char> (nData));
return 1;
}
g_pTracer->Trace (WARNING, "Number too large. (seriell)");
return -1;
}
int CSeriell::ReceiveSeriellData () {
return static_cast<int> (serialGetchar (m_nOwnSeriellAdress));
}