• ESP32 串口读取 jy901s 姿态传感器


    开发平台为 VSCode 和 platformIO,C语言。用到了串口0和串口2

    需要用到一个STM32官方例程中的一个库,我也贴上来了,直接复制本文代码即可用。

     1. JY901.h

    1. // JY901.h
    2. #ifndef JY901_h
    3. #define JY901_h
    4. #define SAVE 0x00
    5. #define CALSW 0x01
    6. #define RSW 0x02
    7. #define RRATE 0x03
    8. #define BAUD 0x04
    9. #define AXOFFSET 0x05
    10. #define AYOFFSET 0x06
    11. #define AZOFFSET 0x07
    12. #define GXOFFSET 0x08
    13. #define GYOFFSET 0x09
    14. #define GZOFFSET 0x0a
    15. #define HXOFFSET 0x0b
    16. #define HYOFFSET 0x0c
    17. #define HZOFFSET 0x0d
    18. #define D0MODE 0x0e
    19. #define D1MODE 0x0f
    20. #define D2MODE 0x10
    21. #define D3MODE 0x11
    22. #define D0PWMH 0x12
    23. #define D1PWMH 0x13
    24. #define D2PWMH 0x14
    25. #define D3PWMH 0x15
    26. #define D0PWMT 0x16
    27. #define D1PWMT 0x17
    28. #define D2PWMT 0x18
    29. #define D3PWMT 0x19
    30. #define IICADDR 0x1a
    31. #define LEDOFF 0x1b
    32. #define GPSBAUD 0x1c
    33. #define YYMM 0x30
    34. #define DDHH 0x31
    35. #define MMSS 0x32
    36. #define MS 0x33
    37. #define AX 0x34
    38. #define AY 0x35
    39. #define AZ 0x36
    40. #define GX 0x37
    41. #define GY 0x38
    42. #define GZ 0x39
    43. #define HX 0x3a
    44. #define HY 0x3b
    45. #define HZ 0x3c
    46. #define Roll 0x3d
    47. #define Pitch 0x3e
    48. #define Yaw 0x3f
    49. #define TEMP 0x40
    50. #define D0Status 0x41
    51. #define D1Status 0x42
    52. #define D2Status 0x43
    53. #define D3Status 0x44
    54. #define PressureL 0x45
    55. #define PressureH 0x46
    56. #define HeightL 0x47
    57. #define HeightH 0x48
    58. #define LonL 0x49
    59. #define LonH 0x4a
    60. #define LatL 0x4b
    61. #define LatH 0x4c
    62. #define GPSHeight 0x4d
    63. #define GPSYAW 0x4e
    64. #define GPSVL 0x4f
    65. #define GPSVH 0x50
    66. #define DIO_MODE_AIN 0
    67. #define DIO_MODE_DIN 1
    68. #define DIO_MODE_DOH 2
    69. #define DIO_MODE_DOL 3
    70. #define DIO_MODE_DOPWM 4
    71. #define DIO_MODE_GPS 5
    72. struct STime
    73. {
    74. unsigned char ucYear;
    75. unsigned char ucMonth;
    76. unsigned char ucDay;
    77. unsigned char ucHour;
    78. unsigned char ucMinute;
    79. unsigned char ucSecond;
    80. unsigned short usMiliSecond;
    81. };
    82. struct SAcc
    83. {
    84. short a[3];
    85. short T;
    86. };
    87. struct SGyro
    88. {
    89. short w[3];
    90. short T;
    91. };
    92. struct SAngle
    93. {
    94. short Angle[3];
    95. short T;
    96. };
    97. struct SMag
    98. {
    99. short h[3];
    100. short T;
    101. };
    102. struct SDStatus
    103. {
    104. short sDStatus[4];
    105. };
    106. struct SPress
    107. {
    108. long lPressure;
    109. long lAltitude;
    110. };
    111. struct SLonLat
    112. {
    113. long lLon;
    114. long lLat;
    115. };
    116. struct SGPSV
    117. {
    118. short sGPSHeight;
    119. short sGPSYaw;
    120. long lGPSVelocity;
    121. };
    122. struct SQuater
    123. {
    124. short q0;
    125. short q1;
    126. short q2;
    127. short q3;
    128. };
    129. struct SSN
    130. {
    131. short sSVNum;
    132. short sPDOP;
    133. short sHDOP;
    134. short sVDOP;
    135. };
    136. class CJY901
    137. {
    138. public:
    139. struct STime stcTime;
    140. struct SAcc stcAcc;
    141. struct SGyro stcGyro;
    142. struct SAngle stcAngle;
    143. struct SMag stcMag;
    144. struct SDStatus stcDStatus;
    145. struct SPress stcPress;
    146. struct SLonLat stcLonLat;
    147. struct SGPSV stcGPSV;
    148. struct SQuater stcQuater;
    149. struct SSN stcSN;
    150. CJY901 ();
    151. void StartIIC();
    152. void StartIIC(unsigned char ucAddr);
    153. void CopeSerialData(unsigned char ucData);
    154. short ReadWord(unsigned char ucAddr);
    155. void WriteWord(unsigned char ucAddr,short sData);
    156. void ReadData(unsigned char ucAddr,unsigned char ucLength,char chrData[]);
    157. void GetTime();
    158. void GetAcc();
    159. void GetGyro();
    160. void GetAngle();
    161. void GetMag();
    162. void GetPress();
    163. void GetDStatus();
    164. void GetLonLat();
    165. void GetGPSV();
    166. private:
    167. unsigned char ucDevAddr;
    168. void readRegisters(unsigned char deviceAddr,unsigned char addressToRead, unsigned char bytesToRead, char * dest);
    169. void writeRegister(unsigned char deviceAddr,unsigned char addressToWrite,unsigned char bytesToRead, char *dataToWrite);
    170. };
    171. extern CJY901 JY901;
    172. #include
    173. #endif

    2. JY901.cpp

    1. // JY901.cpp
    2. #include "JY901.h"
    3. #include "string.h"
    4. CJY901 ::CJY901 ()
    5. {
    6. ucDevAddr =0x50;
    7. }
    8. void CJY901::StartIIC()
    9. {
    10. ucDevAddr = 0x50;
    11. Wire.begin();
    12. }
    13. void CJY901::StartIIC(unsigned char ucAddr)
    14. {
    15. ucDevAddr = ucAddr;
    16. Wire.begin();
    17. }
    18. void CJY901 ::CopeSerialData(unsigned char ucData)
    19. {
    20. static unsigned char ucRxBuffer[250];
    21. static unsigned char ucRxCnt = 0;
    22. ucRxBuffer[ucRxCnt++]=ucData;
    23. if (ucRxBuffer[0]!=0x55)
    24. {
    25. ucRxCnt=0;
    26. return;
    27. }
    28. if (ucRxCnt<11) {return;}
    29. else
    30. {
    31. switch(ucRxBuffer[1])
    32. {
    33. case 0x50: memcpy(&stcTime,&ucRxBuffer[2],8);break;
    34. case 0x51: memcpy(&stcAcc,&ucRxBuffer[2],8);break;
    35. case 0x52: memcpy(&stcGyro,&ucRxBuffer[2],8);break;
    36. case 0x53: memcpy(&stcAngle,&ucRxBuffer[2],8);break;
    37. case 0x54: memcpy(&stcMag,&ucRxBuffer[2],8);break;
    38. case 0x55: memcpy(&stcDStatus,&ucRxBuffer[2],8);break;
    39. case 0x56: memcpy(&stcPress,&ucRxBuffer[2],8);break;
    40. case 0x57: memcpy(&stcLonLat,&ucRxBuffer[2],8);break;
    41. case 0x58: memcpy(&stcGPSV,&ucRxBuffer[2],8);break;
    42. case 0x59: memcpy(&stcQuater,&ucRxBuffer[2],8);break;
    43. case 0x5a: memcpy(&stcSN,&ucRxBuffer[2],8);break;
    44. }
    45. ucRxCnt=0;
    46. }
    47. }
    48. void CJY901::readRegisters(unsigned char deviceAddr,unsigned char addressToRead, unsigned char bytesToRead, char * dest)
    49. {
    50. Wire.beginTransmission(deviceAddr);
    51. Wire.write(addressToRead);
    52. Wire.endTransmission(false); //endTransmission but keep the connection active
    53. Wire.requestFrom(deviceAddr, bytesToRead); //Ask for bytes, once done, bus is released by default
    54. while(Wire.available() < bytesToRead); //Hang out until we get the # of bytes we expect
    55. for(int x = 0 ; x < bytesToRead ; x++)
    56. dest[x] = Wire.read();
    57. }
    58. void CJY901::writeRegister(unsigned char deviceAddr,unsigned char addressToWrite,unsigned char bytesToRead, char *dataToWrite)
    59. {
    60. Wire.beginTransmission(deviceAddr);
    61. Wire.write(addressToWrite);
    62. for(int i = 0 ; i < bytesToRead ; i++)
    63. Wire.write(dataToWrite[i]);
    64. Wire.endTransmission(); //Stop transmitting
    65. }
    66. short CJY901::ReadWord(unsigned char ucAddr)
    67. {
    68. short sResult;
    69. readRegisters(ucDevAddr, ucAddr, 2, (char *)&sResult);
    70. return sResult;
    71. }
    72. void CJY901::WriteWord(unsigned char ucAddr,short sData)
    73. {
    74. writeRegister(ucDevAddr, ucAddr, 2, (char *)&sData);
    75. }
    76. void CJY901::ReadData(unsigned char ucAddr,unsigned char ucLength,char chrData[])
    77. {
    78. readRegisters(ucDevAddr, ucAddr, ucLength, chrData);
    79. }
    80. void CJY901::GetTime()
    81. {
    82. readRegisters(ucDevAddr, 0x30, 8, (char*)&stcTime);
    83. }
    84. void CJY901::GetAcc()
    85. {
    86. readRegisters(ucDevAddr, AX, 6, (char *)&stcAcc);
    87. }
    88. void CJY901::GetGyro()
    89. {
    90. readRegisters(ucDevAddr, GX, 6, (char *)&stcGyro);
    91. }
    92. void CJY901::GetAngle()
    93. {
    94. readRegisters(ucDevAddr, Roll, 6, (char *)&stcAngle);
    95. }
    96. void CJY901::GetMag()
    97. {
    98. readRegisters(ucDevAddr, HX, 6, (char *)&stcMag);
    99. }
    100. void CJY901::GetPress()
    101. {
    102. readRegisters(ucDevAddr, PressureL, 8, (char *)&stcPress);
    103. }
    104. void CJY901::GetDStatus()
    105. {
    106. readRegisters(ucDevAddr, D0Status, 8, (char *)&stcDStatus);
    107. }
    108. void CJY901::GetLonLat()
    109. {
    110. readRegisters(ucDevAddr, LonL, 8, (char *)&stcLonLat);
    111. }
    112. void CJY901::GetGPSV()
    113. {
    114. readRegisters(ucDevAddr, GPSHeight, 8, (char *)&stcGPSV);
    115. }
    116. CJY901 JY901 = CJY901();

    3. main.cpp

    1. // main.cpp
    2. #include
    3. #include
    4. #include
    5. #include
    6. void setup()
    7. {
    8. Serial.begin(9600);
    9. Serial2.begin(9600);
    10. }
    11. void loop()
    12. {
    13. //print received data. Data was received in serialEvent;
    14. Serial.print("Time:20");
    15. Serial.print(JY901.stcTime.ucYear);
    16. Serial.print("-");
    17. Serial.print(JY901.stcTime.ucMonth);
    18. Serial.print("-");
    19. Serial.print(JY901.stcTime.ucDay);
    20. Serial.print(" ");Serial.print(JY901.stcTime.ucHour);
    21. Serial.print(":");Serial.print(JY901.stcTime.ucMinute);
    22. Serial.print(":");
    23. Serial.println((float)JY901.stcTime.ucSecond+(float)JY901.stcTime.usMiliSecond/1000);
    24. Serial.print("Acc:");
    25. Serial.print((float)JY901.stcAcc.a[0]/32768*16);
    26. Serial.print(" ");
    27. Serial.print((float)JY901.stcAcc.a[1]/32768*16);
    28. Serial.print(" ");
    29. Serial.println((float)JY901.stcAcc.a[2]/32768*16);
    30. Serial.print("Gyro:");
    31. Serial.print((float)JY901.stcGyro.w[0]/32768*2000);
    32. Serial.print(" ");
    33. Serial.print((float)JY901.stcGyro.w[1]/32768*2000);
    34. Serial.print(" ");
    35. Serial.println((float)JY901.stcGyro.w[2]/32768*2000);
    36. Serial.print("Angle:");
    37. Serial.print((float)JY901.stcAngle.Angle[0]/32768*180);
    38. Serial.print(" ");
    39. Serial.print((float)JY901.stcAngle.Angle[1]/32768*180);
    40. Serial.print(" ");
    41. Serial.println((float)JY901.stcAngle.Angle[2]/32768*180);
    42. Serial.print("Mag:");
    43. Serial.print(JY901.stcMag.h[0]);
    44. Serial.print(" ");
    45. Serial.print(JY901.stcMag.h[1]);
    46. Serial.print(" ");
    47. Serial.println(JY901.stcMag.h[2]);
    48. Serial.print("Pressure:");
    49. Serial.print(JY901.stcPress.lPressure);
    50. Serial.print(" ");
    51. Serial.println((float)JY901.stcPress.lAltitude/100);
    52. Serial.print("DStatus:");
    53. Serial.print(JY901.stcDStatus.sDStatus[0]);
    54. Serial.print(" ");
    55. Serial.print(JY901.stcDStatus.sDStatus[1]);
    56. Serial.print(" ");
    57. Serial.print(JY901.stcDStatus.sDStatus[2]);
    58. Serial.print(" ");
    59. Serial.println(JY901.stcDStatus.sDStatus[3]);
    60. Serial.print("Longitude:");
    61. Serial.print(JY901.stcLonLat.lLon/10000000);
    62. Serial.print("Deg");
    63. Serial.print((double)(JY901.stcLonLat.lLon % 10000000)/1e5);
    64. Serial.print("m Lattitude:");
    65. Serial.print(JY901.stcLonLat.lLat/10000000);
    66. Serial.print("Deg");
    67. Serial.print((double)(JY901.stcLonLat.lLat % 10000000)/1e5);
    68. Serial.println("m");
    69. Serial.print("GPSHeight:");
    70. Serial.print((float)JY901.stcGPSV.sGPSHeight/10);
    71. Serial.print("m GPSYaw:");
    72. Serial.print((float)JY901.stcGPSV.sGPSYaw/10);
    73. Serial.print("Deg GPSV:");
    74. Serial.print((float)JY901.stcGPSV.lGPSVelocity/1000);
    75. Serial.println("km/h");
    76. Serial.print("SN:");
    77. Serial.print(JY901.stcSN.sSVNum);
    78. Serial.print(" PDOP:");
    79. Serial.print((float)JY901.stcSN.sPDOP/100);
    80. Serial.print(" HDOP:");
    81. Serial.print((float)JY901.stcSN.sHDOP/100);
    82. Serial.print(" VDOP:");
    83. Serial.println((float)JY901.stcSN.sVDOP/100);
    84. Serial.println("");
    85. delay(2000);
    86. while (Serial2.available())
    87. {
    88. JY901.CopeSerialData(Serial2.read());
    89. }
    90. }

    有问题请留言~

  • 相关阅读:
    社区投稿| 以安全视角,深度剖析 Sui Staking 与 LSD
    java遍历文件夹并生成_sidebar.md
    sql创建临时表,获取查询数据后删除临时表,清理空间
    Java进阶(八)Stream、异常体系
    YOLOv8改进 | 注意力机制 | 添加混合局部通道注意力——MLCA【原理讲解】
    一文解读功率放大器(功率放大器如何选型)
    Ubuntu1804安装ROS Melodic
    蔚蓝资源包和数据分析
    15.Django总结
    微信快捷回复软件
  • 原文地址:https://blog.csdn.net/baidu_41750439/article/details/126093926