Newer
Older
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
#include "tourelle.h"
#include <string.h>
#define min(a,b) a<b?a:b;
#define USART_BAUDRATE 9600
#define STAT_OUT 0
#define STAT_INMAIN 1
#define STAT_DIST 2
#define STAT_ANGHUND 3
#define STAT_ANG 4
///////////////////
// PositionsList //
///////////////////
PositionsList::PositionsList():m_usedSize(0),m_allocatedSize(0),m_array(0)
{
reserve(5);
}
PositionsList::PositionsList(const PositionsList& l):m_usedSize(0),m_allocatedSize(0),m_array(0)
{
reserve(l.m_allocatedSize);
m_usedSize=l.m_usedSize;
memcpy(m_array,l.m_array,m_usedSize);
}
void PositionsList::reserve(unsigned int i)
{
PositionData* ptr = new PositionData[i];
memset(ptr, 0, i*sizeof(PositionData));
m_usedSize = min(m_usedSize, i);
m_allocatedSize=i;
memcpy(ptr, m_array, m_usedSize*sizeof(PositionData));
delete m_array;
m_array = ptr;
}
void PositionsList::append(const PositionData& element)
{
if(m_usedSize == m_allocatedSize)
reserve((m_allocatedSize+2)*2);
m_array[m_usedSize]=element;
m_usedSize++;
}
unsigned int PositionsList::size() const
{
return m_usedSize;
}
void PositionsList::clear()
{
m_usedSize=0;
}
void PositionsList::copyTo(PositionsList& l)
{
unsigned int us = l.m_usedSize;
l.m_usedSize = m_usedSize;
m_usedSize = us;
unsigned int as = l.m_allocatedSize;
l.m_allocatedSize = m_allocatedSize;
m_allocatedSize = as;
PositionData* ar = l.m_array;
l.m_array = m_array;
m_array = ar;
}
PositionData& PositionsList::operator[](unsigned int i)
{
return m_array[i];
}
const PositionData& PositionsList::operator[](unsigned int i) const
{
return (*this)[i];
}
////////////////////
Victor Dubois
committed
// Tourelle //
////////////////////
Victor Dubois
committed
void Tourelle::initClocksAndPortsGPIO(uint32_t usart_rcc_index, uint32_t usart_af, GPIO_TypeDef* GPIOx_RX, uint16_t GPIO_Pin_RX, GPIO_TypeDef* GPIOx_TX, uint16_t GPIO_Pin_TX)
#ifdef STM32F40_41xxx // For stm32 h405
RCC_APB1PeriphClockCmd(usart_rcc_index, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOx_TX, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOx_RX, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOx_RX, GPIO_Pin_RX, usart_af);
GPIO_PinAFConfig(GPIOx_TX, GPIO_Pin_TX, usart_af);
#endif
#ifdef STM32F10X_CL // For stm32 h107
RCC_APB2PeriphClockCmd(usart_rcc_index, ENABLE);
GPIO_PinRemapConfig(usart_af, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
// pin TX :
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // port's refresh rate
GPIO_Init(GPIOx_TX, &GPIO_InitStructure);
// pin RX :
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // port's refresh rate
GPIO_Init(GPIOx_RX, &GPIO_InitStructure);
#endif
Victor Dubois
committed
void Tourelle::initUART(USART_TypeDef* usart_index, int baudRate)
#ifdef ROBOTHW
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = baudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(usart_index, &USART_InitStructure);
USART_Cmd(usart_index, ENABLE);
#endif
}
Victor Dubois
committed
Tourelle* Tourelle::getSingleton()
Victor Dubois
committed
static Tourelle* ptr = NULL;
Victor Dubois
committed
ptr = new Tourelle();
return ptr;
}
Victor Dubois
committed
Tourelle::Tourelle()
{
#ifdef ROBOTHW
initClocksAndPortsGPIO(RCC_APB1Periph_USART2, GPIO_Remap_USART2, GPIOC, GPIO_Pin_12, GPIOD, GPIO_Pin_2);
initUART(TURRET_USART_INDEX, USART_BAUDRATE);
Victor Dubois
committed
void Tourelle::reset()
{
m_currentList.copyTo(m_closedList);
m_currentList.clear();
Victor Dubois
committed
unsigned int Tourelle::beaconsDetected()
{
return m_closedList.size();
}
Victor Dubois
committed
PositionData Tourelle::getPositionData(unsigned int idx)
{
return m_closedList.get(idx);
}
PositionData PositionsList::get(unsigned int idx)
return m_array[idx];
}
Victor Dubois
committed
PositionsList Tourelle::getPositionsList() const
{
return m_closedList;
}
Victor Dubois
committed
bool Tourelle::dataAvailable()
#ifdef ROBOTHW
return TURRET_USART_INDEX->SR & USART_FLAG_RXNE;
#else
return false;
#endif
}
Victor Dubois
committed
unsigned int Tourelle::receiveData()
{
#ifdef ROBOTHW
while (!(TURRET_USART_INDEX->SR & USART_FLAG_RXNE));
return ((unsigned int)(TURRET_USART_INDEX->DR & 0x1FF));
#else
return 0;
#endif
}
Victor Dubois
committed
void Tourelle::update()
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{
while (dataAvailable())
{
switch(stat)
{
case STAT_OUT :
{
unsigned int mem = receiveData();
if (mem==250)
{
stat++;
}
else
{
if (mem==254)
reset();
}
}
break;
case STAT_INMAIN :
{
tempdist=receiveData();
stat++;
}
break;
case STAT_DIST :
{
angstr = receiveData();
stat++;
}
break;
case STAT_ANGHUND :
{
tempang=(256*angstr)+receiveData();
stat++;
}
break;
case STAT_ANG :
if (receiveData()==255)
{
PositionData dat;
dat.distance=tempdist;
dat.angle=tempang;
m_currentList.append(dat);
stat=STAT_OUT;
}
else
{
stat=STAT_OUT;
}
break;
default:
m_currentList.clear();
}
}