#include <NRF24.h>
#include <SPI.h>
#include <RcTrainer.h>
#pragma pack(1);
typedef struct
{
float roll;
float pitch;
float yaw;
uint16_t thrust;
} CommanderCrtpValues;
#pragma pack()
#define CRTP_HEADER(port, channel) (((port & 0x0F) << 4) | (channel & 0x0F))
#define CRTP_HEADER_PORT(h) ((h >> 4) & 0xf)
#define CRTP_HEADER_CHANNEL(h) (h & 0x3)
#define PARAM_TOC_CH 0
#define PARAM_READ_CH 1
#define PARAM_WRITE_CH 2
#define LOG_TOC_CH 0
#define LOG_CONTROL_CH 1
#define LOG_LOG_CH 2
#define LOG_MAX_OPS 64
#define LOG_MAX_BLOCKS 8
typedef enum {
CRTP_PORT_CONSOLE = 0x00,
CRTP_PORT_PARAM = 0x02,
CRTP_PORT_COMMANDER = 0x03,
CRTP_PORT_LOG = 0x05,
CRTP_PORT_LINK = 0x0F,
} CRTPPort;
#define CMD_GET_ITEM 0
#define CMD_GET_INFO 1
uint8_t channel;
uint8_t address[] = { 0xe7, 0xe7, 0xe7, 0xe7, 0xe7 };
uint32_t failed_packet_count = 0;
RcTrainer rc;
void setup()
{
Serial.begin(115200);
while (!Serial)
;
Serial.println("NRF24 init failed");
Serial.println("setChannel failed");
Serial.println("setRF failed");
nrf24.
setConfiguration(NRF24_MASK_RX_DR | NRF24_MASK_TX_DS | NRF24_MASK_MAX_RT | NRF24_EN_CRC | NRF24_CRCO);
Serial.println("setRetry failed");
Serial.println("setTransmitAddress failed");
Serial.println("initialised");
}
void dump(char* prompt, uint8_t* data, uint8_t len)
{
Serial.print(prompt);
Serial.print(": ");
for (int i = 0; i < len; i++)
{
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.println("");
}
boolean scan()
{
uint8_t msg[] = { CRTP_HEADER(CRTP_PORT_LINK, 3) };
for (channel = 0; channel < 125; channel++)
{
if (!nrf24.
send(msg,
sizeof(msg)))
Serial.println("scan send failed");
return true;
}
return false;
}
void send(uint8_t* data, uint8_t len)
{
if (!nrf24.
send(data, len))
Serial.println("send failed");
{
}
else
{
failed_packet_count++;
}
}
void loop()
{
while (!scan())
;
failed_packet_count = 0;
Serial.print("found a crazyflie on channel: ");
Serial.println(channel);
while (failed_packet_count < 50)
{
uint8_t buf[32];
buf[0] = CRTP_HEADER(CRTP_PORT_COMMANDER, 0);
CommanderCrtpValues* msg = (CommanderCrtpValues*)(buf+1);
msg->roll = map(rc.getChannel(1), 0, 1023, 30.0, -30.0);
msg->pitch = map(rc.getChannel(2), 0, 1023, 30.0, -30.0);
msg->yaw = map(rc.getChannel(3), 0, 1023, 200.0, -200.0);
msg->thrust = map(rc.getChannel(0), 0, 1023, 0, 65000);
send(buf, sizeof(CommanderCrtpValues) + 1);
delay(20);
}
Serial.println("connection failed");
}