gpt4 book ai didi

c++ - 使用 union 通过SPI将 float 从一个arduino发送到另一个arduino

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:25:13 24 4
gpt4 key购买 nike

我正在尝试使用 union 通过 SPI 将 float 从一个 arduino 发送到另一个。我知道我的接线是正确的,因为我可以发送整数。该代码对我来说看起来是正确的,所以我一定遗漏了一些东西。当我运行代码时,串行监视器只打印出 0.0000。

这是我的主代码

#include <SPI.h>
#include "pins_arduino.h"

void setup ()
{
Serial.begin (115200);

digitalWrite(SS, HIGH); // ensure SS stays high for now

// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin ();

// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);
}

byte transferAndWait (const byte what)
{
byte a = SPI.transfer (what);
delayMicroseconds (10);
return a;
}

union first_union
{
float f;
byte b[4];
}data;

float yaw, pitch, roll, alt, temp;

void loop (void)
{
digitalWrite(SS, LOW);

//YAW
transferAndWait ('Y');
data.b[0] = transferAndWait ('1');
data.b[1] = transferAndWait ('2');
data.b[2] = transferAndWait ('3');
data.b[3] = transferAndWait ('Z');
yaw = data.f;

//PITCH
transferAndWait ('P');
data.b[0] = transferAndWait ('1');
data.b[1] = transferAndWait ('2');
data.b[2] = transferAndWait ('3');
data.b[3] = transferAndWait ('Z');
pitch = data.f;

//ROLL
transferAndWait ('R');
data.b[0] = transferAndWait ('1');
data.b[1] = transferAndWait ('2');
data.b[2] = transferAndWait ('3');
data.b[3] = transferAndWait ('Z');
roll = data.f;

//TEMP
transferAndWait ('T');
data.b[0] = transferAndWait ('1');
data.b[1] = transferAndWait ('2');
data.b[2] = transferAndWait ('3');
data.b[3] = transferAndWait ('Z');
temp = data.f;

//ALT
transferAndWait ('A');
data.b[0] = transferAndWait ('1');
data.b[1] = transferAndWait ('2');
data.b[2] = transferAndWait ('3');
data.b[3] = transferAndWait ('Z');
alt = data.f;

digitalWrite(SS, HIGH);

Serial.print("yaw:");
Serial.println(yaw, 4);
Serial.print("pitch: ");
Serial.println(pitch,4);
Serial.print("roll:");
Serial.println(roll,4);
Serial.print("alt: ");
Serial.println(alt,4);
Serial.print("temp: ");
Serial.println(temp,4);

delay(100);
}

和我的奴隶代码

#include <EEPROM.h>
#include <FreeIMU.h>
#include <Wire.h>
#include <SPI.h>
#include <HMC58X3.h>
#include <MS561101BA.h>
#include <I2Cdev.h>
#include <MPU60X0.h>
#include "pins_arduino.h"

//Set the default object
FreeIMU my3IMU = FreeIMU();
float ypr[3];
float alt = 0;
float temp = 0;

void setup()
{
//Setup IMU
Wire.begin();
delay(500);
my3IMU.init(true);
delay(500);

// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);

// turn on SPI in slave mode
SPCR |= _BV(SPE);

// turn on interrupts
SPCR |= _BV(SPIE);
}

union first_union
{
float f;
byte b[4];
}data;

ISR (SPI_STC_vect)
{
byte command = SPDR;

switch (command)
{
case 'Z':
data.f = 0;
SPDR = 0;
case 'Y':
data.f = ypr[0];
SPDR = data.b[0];
break;
case 'P':
data.f = ypr[2];
SPDR = data.b[0];
break;
case 'R':
data.f = ypr[1];
SPDR = data.b[0];
break;
case 'A':
data.f = alt;
SPDR = data.b[0];
break;
case 'T':
data.f = temp;
SPDR = data.b[0];
break;
case 1:
SPDR = data.b[1];
break;
case 2:
SPDR = data.b[2];
break;
case 3:
SPDR = data.b[3];
break;
}
}

void loop()
{
//my3IMU.getYawPitchRoll(ypr);
//alt = my3IMU.getBaroAlt();
//temp = my3IMU.getBaroTemp();

ypr[0] = 1.1111;
ypr[1] = 2.2222;
ypr[2] = 3.3333;

alt = 4.4444;
temp = 5.5555;
}

有什么想法吗?

谢谢

最佳答案

您正在发送 '1''2''3'(即 ASCII 字符),但开关寻找 123(即数字)。

此外,您在 'Z' 的处理程序中缺少一个 break

关于c++ - 使用 union 通过SPI将 float 从一个arduino发送到另一个arduino,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20409799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com