Skip to content

Commit

Permalink
change to use NEC920 lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Luftalian committed Oct 22, 2023
1 parent 3c8c657 commit 64a56d6
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ src/*
!src/main.cpp
!src/*/
!src/README.md

src/RTD_PARAM.h
9 changes: 7 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4 }",
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0 }",
"editor.formatOnSave": true,
"editor.formatOnType": true,
"C_Cpp.errorSquiggles": "disabled",
Expand Down Expand Up @@ -52,6 +52,11 @@
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"chrono": "cpp",
"condition_variable": "cpp",
"ratio": "cpp",
"mutex": "cpp",
"thread": "cpp"
},
}
1 change: 1 addition & 0 deletions RFparam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Avi_71L_RTD_param
19 changes: 19 additions & 0 deletions RFparam/RTD_PARAM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#ifndef RTD_PARAM_H
#define RTD_PARAM_H

namespace rtdRFparam
{
constexpr uint8_t DST_1 = 0xC2;
constexpr uint8_t DST_2 = 0xA0;
constexpr uint8_t DST_3 = 0x03;
constexpr uint8_t DST_4 = 0xB1;

constexpr uint8_t POWER = 3;
constexpr uint8_t CHANNEL = 37;
constexpr uint8_t RF_BAND = 10;
constexpr uint8_t CS_MODE = 5;
}

#endif
2 changes: 1 addition & 1 deletion lib
5 changes: 2 additions & 3 deletions src/com/gimbal/receive.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ IRAM_ATTR void GimbalReceive(void *parameters) {
while (!Serial2)
;
char command[1];
uint8_t GimbalMode = 0;
// uint8_t GimbalMode = 0;
for (;;) {
switch (GimbalMode) {
case 0: // コマンドを受け取るモード
Expand Down Expand Up @@ -65,8 +65,7 @@ IRAM_ATTR void GimbalReceive(void *parameters) {
default:
break;
}
vTaskDelayUntil(&xLastWakeTime,
GimbalPeriod / portTICK_PERIOD_MS); // 1ms = 1000Hz
vTaskDelayUntil(&xLastWakeTime, GimbalPeriod / portTICK_PERIOD_MS); // 1ms = 1000Hz
}
}

Expand Down
48 changes: 38 additions & 10 deletions src/com/ground/receive.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,46 @@

#include <Arduino.h>

#include <NEC920.hpp>

#include "../../domain/setting.h"
#include "../RFparam/RTD_PARAM.h"
#include "com/gimbal/send.h"

#define GroundPeriod 1

#define GROUND_COMMAND_START 'a'
#define GROUND_COMMAND_STOP 'b'

uint8_t j = 0x71;

IRAM_ATTR void GroundReceive(void *parameters) {
portTickType xLastWakeTime = xTaskGetTickCount();
Serial3.begin(9600);
while (!Serial3)
;
if (!isNec920Setup) {
nec920.beginSerial(&Serial1, 38400, pin920Rx, pin920Tx); // 38400
nec920.setPin(pin920Reset, pin920Wakeup, pin920Mode);
while (nec920.isBootFinished(400000) == 0)
;
nec920.setRfConf(j, rtdRFparam::POWER, rtdRFparam::CHANNEL, rtdRFparam::RF_BAND, rtdRFparam::CS_MODE);
isNec920Setup = true;
}
char command[1];
uint8_t GroundMode = 0;
// uint8_t GroundMode = 0;
while ((nec920.isRecieveCmdResult()) && (!nec920.checkCmdResult(j++)))
;
for (;;) {
switch (GroundMode) {
case 0: // 待機モード
if (Serial3.available() > 0) {
Serial3.readBytes(command, 1);
if (nec920.recieve() > 0) {
if (nec920.isRecieveCmdData()) {
uint8_t tmpArr[256];
uint8_t datalength = nec920.getRecieveData(tmpArr);
// for (int i = 0; i < datalength; i++) {
// Serial.printf("%02X,", tmpArr[i]);
// }
// datalengthが1だと仮定して
command[0] = tmpArr[0];
}
// Ground_command(command, GroundMode);
{
switch (command[0]) {
Expand All @@ -45,8 +66,16 @@ IRAM_ATTR void GroundReceive(void *parameters) {
}
break;
case 1: // 記録するモード
if (Serial3.available() > 0) {
Serial3.read(command, 1);
if (nec920.recieve() > 0) {
if (nec920.isRecieveCmdData()) {
uint8_t tmpArr[256];
uint8_t datalength = nec920.getRecieveData(tmpArr);
// for (int i = 0; i < datalength; i++) {
// Serial.printf("%02X,", tmpArr[i]);
// }
// datalengthが1だと仮定して
command[0] = tmpArr[0];
}
// Ground_command(command, GroundMode);
{
switch (command[0]) {
Expand All @@ -70,8 +99,7 @@ IRAM_ATTR void GroundReceive(void *parameters) {
default:
break;
}
vTaskDelayUntil(&xLastWakeTime,
GroundPeriod / portTICK_PERIOD_MS); // 1ms = 1000Hz
vTaskDelayUntil(&xLastWakeTime, GroundPeriod / portTICK_PERIOD_MS); // 1ms = 1000Hz
}
}

Expand Down
35 changes: 24 additions & 11 deletions src/com/ground/send.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,40 @@

#include <Arduino.h>

#include <NEC920.hpp>

#include "../../domain/setting.h"
#include "../RFparam/RTD_PARAM.h"
#include "domain/setting.h"

#define GroundSendPeriod 1
#define GroundSendPeriod 40

IRAM_ATTR void GroundSend(void *parameters) {
portTickType xLastWakeTime = xTaskGetTickCount();
Serial3.begin(9600);
while (!Serial3)
;
if (!isNec920Setup) {
nec920.beginSerial(&Serial1, 38400, pin920Rx, pin920Tx); // 38400
nec920.setPin(pin920Reset, pin920Wakeup, pin920Mode);
while (nec920.isBootFinished(400000) == 0)
;
nec920.setRfConf(j, rtdRFparam::POWER, rtdRFparam::CHANNEL, rtdRFparam::RF_BAND, rtdRFparam::CS_MODE);
isNec920Setup = true;
}
for (;;) {
std::unique_lock<std::mutex> lock(sendDataMutex);
for (int i = 0; i < 256; i++) {
// 送信
Serial3.write(Send_Data[i]);
// 配列の初期化
Send_Data[i] = 0;
// for (int i = 0; i < 256; i++) {
// // 送信
// Serial3.write(Send_Data[i]);
// // 配列の初期化
// Send_Data[i] = 0;
// }
if (nec920.canSendMsgCheck()) {
uint8_t dstID[4] = {rtdRFparam::DST_1, rtdRFparam::DST_2, rtdRFparam::DST_3, rtdRFparam::DST_4};
// uint8_t parameter[1] = {0x71};
nec920.sendTxCmd(0x13, j, dstID, Send_Data, 1);
}
lock.unlock();
Gimbal_data_count = 0;
vTaskDelayUntil(&xLastWakeTime,
GroundSendPeriod / portTICK_PERIOD_MS); // 1ms = 1000Hz
vTaskDelayUntil(&xLastWakeTime, GroundSendPeriod / portTICK_PERIOD_MS); // 40ms = 25Hz
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/domain/accel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,31 @@
class accel {
public:
accel() {
icm20948 = std::make_shared<ICM>();
icm20602 = std::make_shared<ICM>();
// ICM20948の初期化
icm20948->begin(&SPIC2, ICMCS, SPIFREQ);
icm20602->begin(&SPIC2, ICMCS, SPIFREQ);
}

void Get(uint8_t *SPI_FlashBuff, int CountSPIFlashDataSetExistInBuff);

private:
std::shared_ptr<ICM> icm20948;
std::shared_ptr<ICM> icm20602;
};

void accel::Get(uint8_t *SPI_FlashBuff, int CountSPIFlashDataSetExistInBuff) {
int16_t Icm20948ReceiveData[6] = {};
uint8_t Icm20948_rx_buf[12] = {};
// 加速度をとる
icm20948->Get(Icm20948ReceiveData, Icm20948_rx_buf);
icm20602->Get(Icm20948ReceiveData, Icm20948_rx_buf);

// ICM20948の加速度をとる
for (int index = 10; index < 16; index++) {
SPI_FlashBuff[32 * CountSPIFlashDataSetExistInBuff + index] =
Icm20948_rx_buf[index - 10];
SPI_FlashBuff[32 * CountSPIFlashDataSetExistInBuff + index] = Icm20948_rx_buf[index - 10];
}

// ICM20948の角速度をとる
for (int index = 16; index < 22; index++) {
SPI_FlashBuff[32 * CountSPIFlashDataSetExistInBuff + index] =
Icm20948_rx_buf[index - 10];
SPI_FlashBuff[32 * CountSPIFlashDataSetExistInBuff + index] = Icm20948_rx_buf[index - 10];
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/domain/pressure.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ class pressure {
std::shared_ptr<LPS> lps25;
};

void pressure::Get(uint8_t *SPI_FlashBuff,
int CountSPIFlashDataSetExistInBuff) {
void pressure::Get(uint8_t *SPI_FlashBuff, int CountSPIFlashDataSetExistInBuff) {
uint8_t lps_rx[3] = {};
// LPSの気圧をとる
lps25->Get(lps_rx);
for (int index = 28; index < 31; index++) {
SPI_FlashBuff[32 * CountSPIFlashDataSetExistInBuff + index] =
lps_rx[index - 28];
SPI_FlashBuff[32 * CountSPIFlashDataSetExistInBuff + index] = lps_rx[index - 28];
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/domain/setting.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef SETTING_H
#define SETTING_H

#include <NEC920.hpp>
#include <mutex>

// ピンの定義
Expand All @@ -19,9 +20,18 @@
#define LEDPIN 18
#define SPIFREQ 5000000

const int pin920Rx = 18;
const int pin920Tx = 19;
const int pin920Reset = 21;
const int pin920Wakeup = 22;
const int pin920Mode = 23;

SPICREATE::SPICreate SPIC1;
SPICREATE::SPICreate SPIC2;

NEC920 nec920;
bool isNec920Setup = false;

uint8_t GroundMode = 0;
uint8_t GimbalMode = 0;

Expand Down
12 changes: 4 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,14 @@ void setup() {
delay(1);

// DataWrite関数を起動
xTaskCreateUniversal(DataWrite, "DataWrite", 8192, NULL, 1, &taskHandle[0],
PRO_CPU_NUM);
xTaskCreateUniversal(DataWrite, "DataWrite", 8192, NULL, 1, &taskHandle[0], PRO_CPU_NUM);

// 通信用関数を起動
// 受信用
xTaskCreateUniversal(GimbalReceive, "GimbalReceive", 8192, NULL, 1,
&taskHandle[1], APP_CPU_NUM);
xTaskCreateUniversal(GroundReceive, "GroundReceive", 8192, NULL, 1,
&taskHandle[2], APP_CPU_NUM);
xTaskCreateUniversal(GimbalReceive, "GimbalReceive", 8192, NULL, 1, &taskHandle[1], APP_CPU_NUM);
xTaskCreateUniversal(GroundReceive, "GroundReceive", 8192, NULL, 1, &taskHandle[2], APP_CPU_NUM);
// 送信用
xTaskCreateUniversal(GroundSend, "GroundSend", 8192, NULL, 1,
&taskHandle[3], APP_CPU_NUM);
xTaskCreateUniversal(GroundSend, "GroundSend", 8192, NULL, 1, &taskHandle[3], APP_CPU_NUM);
}

void loop() {}
9 changes: 4 additions & 5 deletions src/usecase/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class formatter {
public:
formatter() {
Lps25 = std::make_shared<pressure>();
icm20948 = std::make_shared<accel>();
icm20602 = std::make_shared<accel>();
timer = std::make_shared<Log67Timer>();
}

Expand All @@ -43,7 +43,7 @@ class formatter {
// LPS25HBの気圧をとるクラスのインスタンス化
std::shared_ptr<pressure> Lps25;
// ICM20948の加速度をとるクラスのインスタンス化
std::shared_ptr<accel> icm20948;
std::shared_ptr<accel> icm20602;

// Timerクラスのインスタンス化
std::shared_ptr<Log67Timer> timer;
Expand All @@ -59,11 +59,10 @@ void formatter::Format() {
}
Record_time = timer->Gettime_record();
for (int index = 0; index < 4; index++) {
SPI_FlashBuff[32 * CountSPIFlashDataSetExistInBuff + index] =
0xFF & (Record_time >> (8 * index));
SPI_FlashBuff[32 * CountSPIFlashDataSetExistInBuff + index] = 0xFF & (Record_time >> (8 * index));
}

icm20948->Get(SPI_FlashBuff, CountSPIFlashDataSetExistInBuff);
icm20602->Get(SPI_FlashBuff, CountSPIFlashDataSetExistInBuff);

// LPSの気圧をとる
if (count_lps % 20 == 0) {
Expand Down
3 changes: 1 addition & 2 deletions src/usecase/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ void Ground_Send_Stack(char *command_data) {
}
// Gimbalデータが1回で送れる量以下のとき
if (Gimbal_data_count < OnceNumberOfGimbalData) {
Send_Data[OnceNumberOfDataExceptGimbalData + Gimbal_data_count] =
command_data[0];
Send_Data[OnceNumberOfDataExceptGimbalData + Gimbal_data_count] = command_data[0];
Gimbal_data_count++;
} else { // 1回で送れる量以上にGimbalデータがあるとき
// cashが満タンでないとき
Expand Down
30 changes: 30 additions & 0 deletions src/usecase/test_stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "../src/usecase/stack.h"
#include "gtest/gtest.h"

TEST(Ground_Send_Stack_Test, Test1) {
char command_data[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
Ground_Send_Stack(command_data);
// TODO: Add your assertions here
}

TEST(Ground_Send_Stack_Test, Test2) {
char command_data[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
Ground_Send_Stack(command_data);
// TODO: Add your assertions here
}

TEST(Ground_Send_Stack_Test, Test3) {
char command_data[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
for (int i = 0; i < 100; i++) {
Ground_Send_Stack(command_data);
}
// TODO: Add your assertions here
}

TEST(Ground_Send_Stack_Test, Test4) {
char command_data[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
for (int i = 0; i < 1000; i++) {
Ground_Send_Stack(command_data);
}
// TODO: Add your assertions here
}
Loading

0 comments on commit 64a56d6

Please sign in to comment.