本項目主要以啟明6M5開發(fā)板作為主控,通過AMG8833模塊獲取手部的溫度,然后通過BP神經(jīng)網(wǎng)絡(luò)解析溫度數(shù)據(jù),來識別手部動作。當(dāng)手部動作和預(yù)定控制指令激活動作相匹配時,向外部設(shè)備發(fā)送控制指令,當(dāng)外部設(shè)備接收到對應(yīng)指令執(zhí)行對應(yīng)的操作。
因為該設(shè)備是通過手部溫度作為控制變量,所以項目運行的溫度在28℃攝氏度下(設(shè)備靜態(tài)是經(jīng)過傳感器測量得到的數(shù)據(jù))。手部溫度為33℃左右,手部距離傳感器大概在5cm左右,并且處于傳感器芯片正前方。說明:環(huán)境溫度會影響傳感器的識別。
硬件部分
①設(shè)備型號
野火RA6M5開發(fā)
②外圍設(shè)備
GY- AMG8833 IR 8x8 紅外熱像
1.44寸彩色TFT顯示屏高清IPS LCD液晶屏模塊128*128
③其他配件
面包板 x 1杜邦線若干
④設(shè)備引腳配置
⑤引腳連接
以及串口:
TX : P512
RX: P511
軟件部分
項目完成使用到的軟件有:
e2 studio
vs code
字模軟件 PCtoLCD2013
野火串口調(diào)試助手
軟件部分代碼說明:
1、GY- AMG8833 IR 8x8 紅外熱像儀 驅(qū)動部分代碼說明:
AMG8833模塊使用I2C 通訊協(xié)議:(使用硬件I2C)
下面是模塊是主要的各個功能驅(qū)動函數(shù)
根據(jù)數(shù)據(jù)手冊說明:只要主機向從機發(fā)送0x80指令,從機設(shè)備 會直接 一次性按順序發(fā)送完 溫度柵格點 1-64 的溫度數(shù)據(jù)
其他指令:按照I2C 通訊協(xié)議讀取
I2C 驅(qū)動 .C 文件部分函數(shù)
// 設(shè)置傳感器模式
void AMG88_SetSensorMode(AMG88_OperatingMode Mode)
{
unsigned char buffer[2]={0x00,Mode};
R_SCI_I2C_Write(&g_i2c6_ctrl, buffer, 2, false);
return;
}
// 獲取當(dāng)前傳感器模式
unsigned char AMG88_GetSensorMode(void)
{
unsigned char OperatingModeBuffer=0;
R_SCI_I2C_Write(&g_i2c6_ctrl, 0, 1, true);
R_BSP_SoftwareDelay(2, 1000);
//Read Register data
R_SCI_I2C_Read(&g_i2c6_ctrl, &OperatingModeBuffer, 1, false);
return OperatingModeBuffer;
}
// 重啟傳感器
void AMG88_SensorReset(AMG88_ResetMode Mode)
{
//
unsigned char ResetBuffer[2]={0x01,(unsigned char)Mode};
//unsigned char ResetBuffer=0x30;
R_SCI_I2C_Write(&g_i2c6_ctrl, ResetBuffer, 2, false);
return;
}
// 設(shè)置幀率
void AMG88_SetFrameRate(AMG88_Frame Frame)
{
unsigned char ResetBuffer[2]={0x02,(unsigned char)Frame};
//unsigned char ResetBuffer=0x30;
R_SCI_I2C_Write(&g_i2c6_ctrl, ResetBuffer, 2, false);
return;
}
// 獲取傳感器幀率
unsigned char AMG88_GetFrameRate(void)
{
unsigned char OperatingModeBuffer=0;
unsigned char Address[1]={0x02};
R_SCI_I2C_Write(&g_i2c6_ctrl, Address, 1, true);
R_BSP_SoftwareDelay(2, 1000);
//Read Register data
R_SCI_I2C_Read(&g_i2c6_ctrl, &OperatingModeBuffer, 1, false);
R_BSP_SoftwareDelay(2, 1000);
return OperatingModeBuffer;
}
// 設(shè)置中斷控制寄存器
void AMG88_SetICR(AMG88_ICR_REGISTER ICR)
{
unsigned char ResetBuffer[2]={0x03,(unsigned char)ICR};
//unsigned char ResetBuffer=0x30;
R_SCI_I2C_Write(&g_i2c6_ctrl, ResetBuffer, 2, false);
return;
}
// 獲取中斷控制寄存器的數(shù)據(jù)
unsigned char AMG88_GetICR(void)
{
unsigned char OperatingModeBuffer=0;
unsigned char Address[1]={0x03};
R_SCI_I2C_Write(&g_i2c6_ctrl, Address, 1, true);
// R_SCI_I2C_Write(&g_i2c6_ctrl, 0x03, 1, true);
R_BSP_SoftwareDelay(2, 1000);
//Read Register data
R_SCI_I2C_Read(&g_i2c6_ctrl, &OperatingModeBuffer, 1, false);
return OperatingModeBuffer;
}
// 獲取當(dāng)前傳感器狀態(tài)
unsigned char AMG88_GetStatus(void)
{
unsigned char OperatingModeBuffer=0;
unsigned char Address[1]={0x04};
R_SCI_I2C_Write(&g_i2c6_ctrl, Address, 1, true);
// R_SCI_I2C_Write(&g_i2c6_ctrl, 0x04, 1, true);
R_BSP_SoftwareDelay(2, 1000);
//Read Register data
R_SCI_I2C_Read(&g_i2c6_ctrl, &OperatingModeBuffer, 1, false);
return OperatingModeBuffer;
}
// 清除傳感器標志位
void AMG88_SetStatusClear(AMG_Status_FLAG ClearStatus)
{
unsigned char ResetBuffer[2]={0x05,(unsigned char)ClearStatus};
//unsigned char ResetBuffer=0x30;
R_SCI_I2C_Write(&g_i2c6_ctrl, ResetBuffer, 2, false);
return;
}
//
void AMG88_SetAverage(BOOL Flag)
{
unsigned char ResetBuffer[2]={0x07,(Flag==TRUE)?(0xFF):(0)};
//unsigned char ResetBuffer=0x30;
R_SCI_I2C_Write(&g_i2c6_ctrl, ResetBuffer, 2, false);
}
//
unsigned char AMG88_GetAverage(void)
{
unsigned char OperatingModeBuffer=0;
// R_SCI_I2C_Write(&g_i2c6_ctrl, 0x07, 1, true);
unsigned char Address[1]={0x07};
R_SCI_I2C_Write(&g_i2c6_ctrl, Address, 1, true);
R_BSP_SoftwareDelay(2, 1000);
//Read Register data
R_SCI_I2C_Read(&g_i2c6_ctrl, &OperatingModeBuffer, 1, false);
return OperatingModeBuffer;
}
// 設(shè)置中斷優(yōu)先級
void AMG88_SetILR(unsigned char *ValueBuffer,unsigned char ArrayLenth)
{
unsigned char ResetBuffer[7]={0x08,0x00,0x00,0x00,
0x00,0x00,0x00};
if(ArrayLenth<=7 && ArrayLenth >= 1)
return;
for(unsigned char i= 1 ;i<7;i++)
{
if(i%2==0)
{
ResetBuffer[i]=(0x0F & ValueBuffer[i-1]);
}else
{
ResetBuffer[i]=ValueBuffer[i-1];
}
}
//unsigned char ResetBuffer=0x30;
R_SCI_I2C_Write(&g_i2c6_ctrl, ResetBuffer, ArrayLenth+1, false);
return;
}
unsigned char Tempeture_Flag[2];
// 獲取傳感器 熱敏電阻 電阻值
unsigned short AMG88_GetThermistor(void)
{
unsigned short buffer_flag=0;
unsigned char Address[1]={0x0E};
R_SCI_I2C_Write(&g_i2c6_ctrl, Address, 1, true);
// R_SCI_I2C_Write(&g_i2c6_ctrl, 0x0E, 1, true);
R_BSP_SoftwareDelay(2, 1000);
//Read Register data
R_SCI_I2C_Read(&g_i2c6_ctrl, &Tempeture_Flag[0], 1, false);
R_BSP_SoftwareDelay(2, 1000);
Address[0]=0x0F;
R_SCI_I2C_Write(&g_i2c6_ctrl, Address, 1, true);
R_BSP_SoftwareDelay(2, 1000);
R_SCI_I2C_Read(&g_i2c6_ctrl, &Tempeture_Flag[1], 1, false);
R_BSP_SoftwareDelay(2, 1000);
buffer_flag=Tempeture_Flag[1]<<8;
buffer_flag|=Tempeture_Flag[0];
return buffer_flag;
}
unsigned char Buffer[10];
unsigned char Revice[128];
// 獲取傳感器的溫度
void AMG88_SensorData(void)
{
/*
* register address
*
* */
Buffer[0]=0x80;
//Send slave address
R_SCI_I2C_Write(&g_i2c6_ctrl, Buffer, 1, true);
R_BSP_SoftwareDelay(2, 1000);
//Read Register data
R_SCI_I2C_Read(&g_i2c6_ctrl, Revice, 128, false);
}
2、1.44寸彩色TFT顯示屏高清IPS LCD液晶屏模塊128*128 部分代碼說明
該LCD 液晶屏使用SPI 通訊協(xié)議:(使用模擬SPI)
驅(qū)動芯片為ST7735SPI
驅(qū)動 .C 文件部分函數(shù)
void SPI_init(void)
{
SET_LED();
SET_CS();
SET_CDX();
SET_RST();
SET_CLK();
SET_SDA();
return;
}
void SPI_SendData(unsigned char Data) // CDX = 1
{
unsigned char i;
for (i = 0; i < 8; i++)
{
CLEAR_CLK();
if ((Data & 0x80) != 0)
SET_SDA();
else
CLEAR_SDA();
Data <<= 1;
SET_CLK();
}
return;
}
void SPI_WriteCommand(unsigned char Data) //CDX = 0
{
CLEAR_CS();
CLEAR_CDX();
SPI_SendData (Data);
SET_CS();
return;
}
void SPI_WriteData(unsigned char Data) //CDX = 1
{
CLEAR_CS();
SET_CDX();
SPI_SendData (Data);
SET_CS();
return;
}
void WriteDispData(unsigned char DataH, unsigned char DataL)
{
SPI_SendData (DataH);
SPI_SendData (DataL);
}
void LCD_Init(void)
{
SET_RST();
R_BSP_SoftwareDelay (100, BSP_DELAY_UNITS_MILLISECONDS);
CLEAR_RST();
R_BSP_SoftwareDelay (100, BSP_DELAY_UNITS_MILLISECONDS);
SET_RST();
R_BSP_SoftwareDelay (200, BSP_DELAY_UNITS_MILLISECONDS);
SPI_WriteCommand (0x11); //Exit Sleep
R_BSP_SoftwareDelay (120, BSP_DELAY_UNITS_MILLISECONDS);
SPI_WriteCommand (0xB1);
SPI_WriteData (0x05); //0a
SPI_WriteData (0x3c); //14
SPI_WriteData (0x3c);
SPI_WriteCommand (0xB2);
SPI_WriteData (0x05);
SPI_WriteData (0x3c);
SPI_WriteData (0x3c);
SPI_WriteData (0xB3);
SPI_WriteData (0x05);
SPI_WriteData (0x3c);
SPI_WriteData (0x3c);
SPI_WriteData (0x05);
SPI_WriteData (0x3c);
SPI_WriteData (0x3c);
SPI_WriteCommand (0xB4); // 前面的b1-b5 是設(shè)置幀速率
SPI_WriteData (0x03);
SPI_WriteCommand (0xC0); // Set VRH1[4:0] & VC[2:0] for VCI1 & GVDD Power Control
SPI_WriteData (0x28);
SPI_WriteData (0x08);
SPI_WriteData (0x04);
SPI_WriteCommand (0xC1); // Set BT[2:0] for AVDD & VCL & VGH & VGL
SPI_WriteData (0xC0);
SPI_WriteCommand (0xC2); // Set VMH[6:0] & VML[6:0] for VOMH & VCOML
SPI_WriteData (0x0D); //54h
SPI_WriteData (0x00); //33h
SPI_WriteCommand (0xC3);
SPI_WriteData (0x8D);
SPI_WriteData (0x2A);
SPI_WriteCommand (0xC4);
SPI_WriteData (0x8D);
SPI_WriteData (0xEE);
SPI_WriteCommand (0xC5);
SPI_WriteData (0x1A);
SPI_WriteCommand (0x36); //MX,MY,RGB MODE
SPI_WriteData (0x08);
SPI_WriteCommand (0xe0);
SPI_WriteData (0x04); //2c
SPI_WriteData (0x22);
SPI_WriteData (0x07);
SPI_WriteData (0x0A);
SPI_WriteData (0x2E);
SPI_WriteData (0x30);
SPI_WriteData (0x25);
SPI_WriteData (0x2A);
SPI_WriteData (0x28);
SPI_WriteData (0x26);
SPI_WriteData (0x2E);
SPI_WriteData (0x3A);
SPI_WriteData (0x00);
SPI_WriteData (0x01);
SPI_WriteData (0x03);
SPI_WriteData (0x03);
SPI_WriteCommand (0xe1);
SPI_WriteData (0x04);
SPI_WriteData (0x16);
SPI_WriteData (0x06);
SPI_WriteData (0x06);
SPI_WriteData (0x0D);
SPI_WriteData (0x2D);
SPI_WriteData (0x26);
SPI_WriteData (0x23);
SPI_WriteData (0x27);
SPI_WriteData (0x27);
SPI_WriteData (0x25);
SPI_WriteData (0x2D);
SPI_WriteData (0x3B);
SPI_WriteData (0x00);
SPI_WriteData (0x01);
SPI_WriteData (0x04);
SPI_WriteData (0x13);
SPI_WriteCommand (0x3A);
SPI_WriteData (0x05);
SPI_WriteCommand (0x29); // Display On
R_BSP_SoftwareDelay (20, BSP_DELAY_UNITS_MILLISECONDS);
}
void BlockWrite(unsigned short Xstart, unsigned short Xend, unsigned short Ystart, unsigned short Yend)
{
SPI_WriteCommand (0x2A);
SPI_WriteData (Xstart >> 8);
SPI_WriteData (Xstart + 2);
// SPI_WriteData(Xstart);
SPI_WriteData (Xend >> 8);
SPI_WriteData (Xend + 2);
// SPI_WriteData(Xstart);
SPI_WriteCommand (0x2B);
SPI_WriteData (Ystart >> 8);
SPI_WriteData (Ystart + 1);
SPI_WriteData (Yend >> 8);
SPI_WriteData (Yend + 1);
SPI_WriteCommand (0x2c);
}
void DispColor(unsigned short color)
{
unsigned short i, j;
BlockWrite (0, COL - 1, 0, ROW - 1);
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
SPI_WriteData (color >> 8);
SPI_WriteData (color);
// DelayMs(1);
}
}
}
void ClearFullScreen(void)
{
unsigned short i, j;
BlockWrite (0, COL - 1, 0, ROW - 1);
for (j = 0; j < COL; j++)
{
SPI_WriteData (i + 50);
SPI_WriteData (j + 50);
}
return;
}
void DrawColor(unsigned short ColorNumber)
{
SPI_WriteData (0xFF);
SPI_WriteData (0xFF);
return;
}
3、BP 神經(jīng)網(wǎng)絡(luò):
代碼說明:
三層網(wǎng)絡(luò)結(jié)構(gòu):
第一層是輸入層,第二層是隱藏層,第三層是輸出層
神經(jīng)網(wǎng)絡(luò)預(yù)測代碼說明:
神經(jīng)網(wǎng)絡(luò)預(yù)測的原理是,將目標數(shù)據(jù)輸入到神經(jīng)網(wǎng)絡(luò)中,經(jīng)過神經(jīng)網(wǎng)絡(luò)中參數(shù)的迭代,使之得到符合要求的數(shù)據(jù)數(shù)據(jù),然后保存神經(jīng)網(wǎng)絡(luò)中的參數(shù)(各個節(jié)點的權(quán)重參數(shù))。使用該網(wǎng)絡(luò)預(yù)測時,將訓(xùn)練好的參數(shù),導(dǎo)入到神經(jīng)網(wǎng)絡(luò)中,該神經(jīng)網(wǎng)絡(luò)就預(yù)測和神經(jīng)網(wǎng)絡(luò)中相符合的數(shù)據(jù)。
該神經(jīng)網(wǎng)絡(luò)的相關(guān)信息如下:
三層BP神經(jīng)網(wǎng)絡(luò):
輸入層有64個元素 , 隱藏層有34個元素, 輸出層有10個元素
訓(xùn)練次數(shù)為:10000次,最終的錯誤率為:0.00658,學(xué)習(xí)率為:0.1 ,動量因子:0.1 訓(xùn)練數(shù)據(jù)總共160組 (160組中 ,分成三份)總共訓(xùn)練了三個手勢
手勢1 36組數(shù)據(jù) 手勢2 68組數(shù)據(jù) 手勢3 54組數(shù)據(jù) 上圖:為編寫文檔時所拍,非傳感器測量時圖片,僅說明在采集測試數(shù)據(jù)時的手勢動作
訓(xùn)練數(shù)據(jù)示例:
[[0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.5,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000],[0,1,0,0,0,0,0,0,0,0]],
// 手勢 1 要求輸出 結(jié)果 -----> [0,1,0,0,0,0,0,0,0,0]
[[0.0000,0.0000,0.5,0.5,0.0000,0.5,0.5,0.5,0.0000,0.0000,0.5,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.5,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000],[0,0,1,0,0,0,0,0,0,0]],
// 手勢 2 要求輸出 結(jié)果 -----> [0,0,1,0,0,0,0,0,0,0]
[[0.0000,0.0000,0.0000,0.5,0.5,0.5,0.5,0.5,0.0000,0.0000,0.0000,0.5,0.5,0.5,0.0000,0.5,0.0000,0.0000,0.5,0.0000,0.0000,0.5,0.0000,0.5,0.0000,0.5,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.5,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000],[0,0,0,1,0,0,0,0,0,0]]
// 手勢 3 要求輸出 結(jié)果 -----> [0,0,0,1,0,0,0,0,0,0]
// 注:以上數(shù)據(jù)僅為 測試數(shù)據(jù)中的手勢數(shù)據(jù)的 一部分 ,不代表整體數(shù)據(jù)
// 預(yù)測輸出數(shù)據(jù) 示例:
[-0.0023248156377385144, 0.035785164105157696, 0.05889932014156386, 0.9992514065884543, 0.0003713636538696458, -0.002541229896438062, -0.0033772818188316607, -0.0023972941452978813, 0.001043452650557289, -0.0026320033807735485]
輸出數(shù)據(jù)說明:
該網(wǎng)絡(luò)有10個數(shù)據(jù)輸出 ,(如:[0,0,0,1,0,0,0,0,0,0] (從左往右)依次是 0 - 9 手勢 ,但本次訓(xùn)練 僅僅訓(xùn)練了3個手勢, 結(jié)果如上。
其他信息說明:
本次的隱藏層的數(shù)目依次經(jīng)歷了 12->24->128->34 的變化 ,具體的數(shù)目和輸入輸出的元素個數(shù),沒有實際的關(guān)聯(lián)(網(wǎng)上雖然有建議) ,具體看情況而論,因為是三層網(wǎng)絡(luò),隱藏層的數(shù)量不可以太少,也不可以太多,太少,說簡單的,輸出的數(shù)據(jù)不在[0,1]的區(qū)間,太多,輸出的都是0.9左右的數(shù)據(jù)
輸出的數(shù)據(jù)不在[0,1]的區(qū)間
?可以調(diào)整 學(xué)習(xí)率 或者 訓(xùn)練次數(shù)(增加),或者是動量因子(修改該參數(shù)時,學(xué)習(xí)率不變)
調(diào)整隱藏層的節(jié)點數(shù)目(往大了調(diào))?
輸出的都是0.9左右的數(shù)據(jù)(過擬合)
調(diào)整隱藏層的節(jié)點數(shù)目(往小了調(diào))(按實際情況調(diào)節(jié))
輸出數(shù)據(jù)的設(shè)定,按照激活函數(shù)的取值選擇
輸入數(shù)據(jù)的選擇,[0-1]之間 ,為了提供訓(xùn)練的成功率,在輸入數(shù)據(jù)中做了一些處理
訓(xùn)練的前提是保證網(wǎng)絡(luò)正常(代碼沒有寫錯)
優(yōu)化訓(xùn)練的操作說明:
對數(shù)據(jù)進行了非0即0.5 的處理 ,對于超過 特定溫度值的數(shù)據(jù)為0.5 ,不超過為 0(只要有相對應(yīng)的特征即可)
網(wǎng)絡(luò)訓(xùn)練成功的標志,輸出的數(shù)據(jù)在(本網(wǎng)絡(luò))[0,1]之間,并且輸出的數(shù)據(jù) 對應(yīng)符合 輸入的數(shù)據(jù)(只要有符合的即可嘗試在在設(shè)備上運行),建議訓(xùn)練完成的網(wǎng)絡(luò),在預(yù)測時,要同時多預(yù)測幾個,防止是誤差
// 激活函數(shù)
double sigmoid(double x)
{
return tanh(x);
}
// 前向傳播
void Forward()
{
unsigned char i=0,j=0;
double Temp=0.0;
double *InputValueTemp;
InputValueTemp=InputValue;
for( i=0 ;i< HIDDENSIZE ; i++)
{
Temp=0;
for(j=0 ; j < INPUTSIZE ; j++ )
{
Temp+=InputValue[j]*InputWeight[j*HIDDENSIZE+i];
}
HiddenValue[i]=sigmoid(Temp);
}
for( i=0 ;i < OUTPUTSIZE ; i++)
{
Temp=0;
for( j = 0; j < HIDDENSIZE ;j++ )
{
Temp+=HiddenValue[j]*OutputWeight[j*OUTPUTSIZE+i];
}
OutputValue[i]=sigmoid(Temp);
}
}
}
左上角
手勢1 白色
手勢2 淺綠色
手勢3 淺紫色
本項目還有需要優(yōu)化的地方,也有著許多不足。作者水平有限,希望廣大網(wǎng)友批評指正。
審核編輯:湯梓紅
-
傳感器
+關(guān)注
關(guān)注
2550文章
51037瀏覽量
753085 -
mcu
+關(guān)注
關(guān)注
146文章
17123瀏覽量
350995 -
神經(jīng)網(wǎng)絡(luò)
+關(guān)注
關(guān)注
42文章
4771瀏覽量
100719 -
瑞薩
+關(guān)注
關(guān)注
35文章
22308瀏覽量
86241
原文標題:【瑞薩RA MCU創(chuàng)意氛圍賽作品賞析】項目14——手勢識別控制終端
文章出處:【微信號:瑞薩MCU小百科,微信公眾號:瑞薩MCU小百科】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論