summaryrefslogtreecommitdiffstats
path: root/source/Concept/Framework/modules/input/distance_sensor.c
blob: 239e63b1399908a9358c9e90f89a177e511dac0b (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "distance_sensor.h"

//-----------------------------------------------------------------------------
void Distance_Sensor::SetSignalFactor(uint8 factor)
{
  if(factor > 16)
	{
	 	factor = 16;
	}

	uint8 temp[2];
	uint8 state;
	tx_type tx_frame[2];
	
	state = SUCCESS;

	tx_frame[0].slave_adr = this->slaveAddr + W;
	tx_frame[0].size = 2;
	tx_frame[0].data_ptr = temp;
	tx_frame[0].data_ptr[0] = 1;
	tx_frame[0].data_ptr[1] = factor;

	tx_frame[1].slave_adr = OWN_ADR;

	state = Send_to_TWI(tx_frame);
}

//-----------------------------------------------------------------------------
void Distance_Sensor::SetSlaveAddress(uint8 newSlaveAddress)
{
	uint8 temp[2];
	uint8 state;
	tx_type tx_frame[2];
	
	state = SUCCESS;

	tx_frame[0].slave_adr = this->slaveAddr + W;
	tx_frame[0].size = 2;
	tx_frame[0].data_ptr = temp;
	tx_frame[0].data_ptr[0] = 0;		
	tx_frame[0].data_ptr[1] = 160;		
	tx_frame[1].slave_adr = OWN_ADR;
	state = Send_to_TWI(tx_frame);

	msleep(60);

	tx_frame[0].data_ptr[1] = 170;	
	state = Send_to_TWI(tx_frame);

	msleep(60);

	tx_frame[0].data_ptr[1] = 165;	
	state = Send_to_TWI(tx_frame);

	msleep(60);

	tx_frame[0].data_ptr[1] = newSlaveAddress;
	state = Send_to_TWI(tx_frame);
}

//-----------------------------------------------------------------------------
void Distance_Sensor::SetRange(unsigned int millimeters){
	uint8 temp[2];
	uint8 state;
	tx_type tx_frame[2];
	
	state = SUCCESS;

	millimeters = (millimeters/43); 

	tx_frame[0].slave_adr = this->slaveAddr+W;
	tx_frame[0].size = 2;
	tx_frame[0].data_ptr = temp;
	tx_frame[0].data_ptr[0] = 2;
	tx_frame[0].data_ptr[1] = millimeters;

	tx_frame[1].slave_adr = OWN_ADR;

	state = Send_to_TWI(tx_frame);
}

/*!
 * Messung ausloesen
 * @param metric_unit 0x50 in Zoll, 0x51 in cm, 0x52 ms
 * @return Resultat der Aktion
 */

uint8 Distance_Sensor::srf10_ping(uint8 metric_unit){
	uint8 temp[2];
	uint8 state;
	tx_type tx_frame[2];
	
	state = SUCCESS;

	tx_frame[0].slave_adr = this->slaveAddr+W;
	tx_frame[0].size = 2;
	tx_frame[0].data_ptr = temp;
	tx_frame[0].data_ptr[0] = SRF10_COMMAND;
	tx_frame[0].data_ptr[1] = metric_unit;

	tx_frame[1].slave_adr = OWN_ADR;

	state = Send_to_TWI(tx_frame);
	
	return state;
}

//-----------------------------------------------------------------------------
uint8 Distance_Sensor::ReadRegister(uint8 registerToRead){
	uint8 temp;
	uint8 value;
	uint8 state;
	tx_type tx_frame[3];

	state = SUCCESS;
	value = 0;

	tx_frame[0].slave_adr = this->slaveAddr+W;
	tx_frame[0].size = 1;
	tx_frame[0].data_ptr = &temp;
	tx_frame[0].data_ptr[0] = registerToRead;

	tx_frame[1].slave_adr = this->slaveAddr+R;
	tx_frame[1].size = 1;
	tx_frame[1].data_ptr = &value;

	tx_frame[2].slave_adr = OWN_ADR;

	state = Send_to_TWI(tx_frame);
	
	return value;
}

/*!
 * Messung starten Ergebniss aufbereiten und zurueckgeben
 * @return Messergebniss
 */

uint16 Distance_Sensor::srf10_get_measure(){
	char hib;
	char lob;
	char state;

	state = SUCCESS;

	state = srf10_ping(SRF10_CENTIMETERS);
	msleep(10);									//Optimierungs Potential
	lob=ReadRegister(SRF10_LOB);
	msleep(10);									//Optimierungs Potential
	hib=ReadRegister(SRF10_HIB);
	
	return (hib*256)+lob;
}

//-----------------------------------------------------------------------------
uint16 Distance_Sensor::GetDistance()
{
	return srf10_get_measure();
}