我正在尝试Arduino终极GPS突破,我希望从GPS获得经度和纬度。然后我想通过RF无线发送这两个变量。如下图所示:
我使用一个名为panstamp的RF模块的库,可以从Arduino 1发送经度和纬度,并在Arduino 2中接收它们,如下面的代码所示:
发射:
void send_data() {
CCPACKET data;
data.length=2;
float lon=26.533255;
float lat=27.533463;
data.data[0]=lon;
data.data[1]=lat;
if(cc1101.sendData(data)){
Serial.println(data.data[0]);
Serial.println(data.data[1]);
Serial.println(" sent ok ");
return true;
}else{
Serial.println("sent failed ");
return false;
}
}
接收:
void loop(){
float j = 0;
lon = packet.data[j];
Serial.print(lon);
Serial.print(" ");
float k = 1;
lat = packet.data[k];
Serial.print(lat);
Serial.println(".");
}
在发送和接收时它非常有效:)
问题是当我收到这两个变量时,我刚收到 lon 26.00和lat 27.00 但不是 lon 26.533255 lat 27.533463 正如我所料。
我假设的数据类型存在一些错误。我调查了panstamp库以找到改变类型的东西,但没有成功。
这是CCPACKET的头文件:
#ifndef _CCPACKET_H
#define _CCPACKET_H
#include "Arduino.h"
/**
* Buffer and data lengths
*/
#define CC1101_BUFFER_LEN 64
#define CC1101_DATA_LEN CC1101_BUFFER_LEN - 3
/**
* Class: CCPACKET
*
* Description:
* CC1101 data packet class
*/
class CCPACKET
{
public:
/**
* Data length
*/
byte length;
/**
* Data buffer
*/
byte data[CC1101_DATA_LEN];
/**
* CRC OK flag
*/
boolean crc_ok;
/**
* Received Strength Signal Indication
*/
byte rssi;
/**
* Link Quality Index
*/
byte lqi;
};
#endif
以及发送数据/接收数据的源代码:
boolean CC1101::sendData(CCPACKET packet)
{
byte marcState;
bool res = false;
// Declare to be in Tx state. This will avoid receiving packets whilst
// transmitting
rfState = RFSTATE_TX;
// Enter RX state
setRxState();
// Check that the RX state has been entered
while (((marcState = readStatusReg(CC1101_MARCSTATE)) & 0x1F) != 0x0D)
{
if (marcState == 0x11) // RX_OVERFLOW
flushRxFifo(); // flush receive queue
}
delayMicroseconds(500);
// Set data length at the first position of the TX FIFO
writeReg(CC1101_TXFIFO, packet.length);
// Write data into the TX FIFO
writeBurstReg(CC1101_TXFIFO, packet.data, packet.length);
// CCA enabled: will enter TX state only if the channel is clear
setTxState();
// Check that TX state is being entered (state = RXTX_SETTLING)
marcState = readStatusReg(CC1101_MARCSTATE) & 0x1F;
if((marcState != 0x13) && (marcState != 0x14) && (marcState != 0x15))
{
setIdleState(); // Enter IDLE state
flushTxFifo(); // Flush Tx FIFO
setRxState(); // Back to RX state
// Declare to be in Rx state
rfState = RFSTATE_RX;
return false;
}
// Wait for the sync word to be transmitted
wait_GDO0_high();
// Wait until the end of the packet transmission
wait_GDO0_low();
// Check that the TX FIFO is empty
if((readStatusReg(CC1101_TXBYTES) & 0x7F) == 0)
res = true;
setIdleState(); // Enter IDLE state
flushTxFifo(); // Flush Tx FIFO
// Enter back into RX state
setRxState();
// Declare to be in Rx state
rfState = RFSTATE_RX;
return res;
}
byte CC1101::receiveData(CCPACKET * packet)
{
byte val;
byte rxBytes = readStatusReg(CC1101_RXBYTES);
// Any byte waiting to be read and no overflow?
if (rxBytes & 0x7F && !(rxBytes & 0x80))
{
// Read data length
packet->length = readConfigReg(CC1101_RXFIFO);
// If packet is too long
if (packet->length > CC1101_DATA_LEN)
packet->length = 0; // Discard packet
else
{
// Read data packet
readBurstReg(packet->data, CC1101_RXFIFO, packet->length);
// Read RSSI
packet->rssi = readConfigReg(CC1101_RXFIFO);
// Read LQI and CRC_OK
val = readConfigReg(CC1101_RXFIFO);
packet->lqi = val & 0x7F;
packet->crc_ok = bitRead(val, 7);
}
}
else
packet->length = 0;
setIdleState(); // Enter IDLE state
flushRxFifo(); // Flush Rx FIFO
//cmdStrobe(CC1101_SCAL);
// Back to RX state
setRxState();
return packet->length;
}
请有人帮助我:)。
Panstamp库的链接:PanStamp Library
答案 0 :(得分:1)
据我所知,你在这里失去了精确度:
float lon=26.533255;
float lat=27.533463;
data.data[0]=lon;
data.data[1]=lat;
因为数据是一个字节数组,根据:
/**
* Data buffer
*/
byte data[CC1101_DATA_LEN];
您需要正确缓冲数据。
答案 1 :(得分:0)
float lon = 26.533255; byte * p =(byte *)& lon;
for(int i = 0; i< sizeof(lon); i ++){ data.data [i] = p [i]; }
如果它的工作原理与lat相同,或者像floattobyte那样使用函数并使用它。
答案 2 :(得分:0)
HighPredator是对的!
从panstamp lib我们看到CCPACKET :: data字段是一个uint8_t数组: https://github.com/panStamp/panstamp/wiki/CCPACKET#data
基本上当你写:
float lon=26.533255;
float lat=27.533463;
data.data[0]=lon;
data.data[1]=lat;
编译器基本上是这样做的:
data.data[0]=uint8_t(lon); // So 26.533255f just becomes 26
data.data[1]=uint8_t(lat); // So 27.533463just becomes 27
您需要了解浮点类型,它长度为4个字节,因此您需要将数据包设置为8个字节,然后像这样传输原始字节:
data.length = 8;
data.data[0] = ((uint8_t*)(&lon))[0]; // Transfer first byte of the float
data.data[1] = ((uint8_t*)(&lon))[1];
data.data[2] = ((uint8_t*)(&lon))[2];
data.data[3] = ((uint8_t*)(&lon))[3]; // Transfer last byte of the float
data.data[4] = ((uint8_t*)(&lat))[0]; // Transfer first byte of the float
data.data[5] = ((uint8_t*)(&lat))[1];
data.data[6] = ((uint8_t*)(&lat))[2];
data.data[7] = ((uint8_t*)(&lat))[3]; // Transfer last byte of the float
在接收端,您可以像这样重新组合浮点数:
float lon, lat;
((uint8_t*)(&lon))[0] = data.data[0]; // Place first byte
((uint8_t*)(&lon))[1] = data.data[1];
((uint8_t*)(&lon))[2] = data.data[2];
((uint8_t*)(&lon))[3] = data.data[3]; // Place last byte
((uint8_t*)(&lat))[0] = data.data[4]; // Place first byte
((uint8_t*)(&lat))[1] = data.data[5];
((uint8_t*)(&lat))[2] = data.data[6];
((uint8_t*)(&lat))[3] = data.data[7]; // Place last byte
希望有所帮助。