[转载] [Arduino模块]CMPS10 倾斜补偿的电子罗盘

2013-12-181.4万
AI 快速预览详细 收起
CMPS10模块是一款倾斜补偿的电子罗盘,采用3轴磁强计和3轴加速计,输出0-359.9度的方向数据。通过I2C模式,可以读取模块的数据并在LCD03上显示方向、俯仰和横滚值。适用于需要高精度方向测量的项目。
[Arduino模块]
CMPS10模块是一个倾斜补偿的电子罗盘。采用一个3轴磁强计、一个3轴加速计和一个强大的16位处理器,CMPS10一直旨在消除由PCB的倾斜引起的误差 。CMPS10输出的0-3599代表0-359.9或0到255。测量X,Y和z分量的磁场,俯仰和横滚三个传感器的输出被用来计算磁场角度,这些组件也取得了在那里的原始形式 。CMPS10模块需要3.3- 5V的电源以及25毫安电流。

代码示例:
  1. /****************************************************************
  2. *                  Arduino CMPS10 example code                  *
  3. *                    CMPS10 running I2C mode                    *
  4. *                    by James Henderson, 2012                   *
  5. *****************************************************************/
  6. #include <Wire.h>
  7. #include <SoftwareSerial.h>
  8. #define ADDRESS 0x60                                          // Defines address of CMPS10
  9. #define LCD_RX              0x02                              // RX and TX pins used for LCD0303 serial port
  10. #define LCD_TX              0x03
  11. #define LCD03_HIDE_CUR      0x04
  12. #define LCD03_CLEAR         0x0C
  13. #define LCD03_SET_CUR       0x02
  14. SoftwareSerial lcd03 =  SoftwareSerial(LCD_RX, LCD_TX);      // Defines software serial port for LCD03
  15. void setup(){
  16.   Wire.begin();                                               // Conects I2C
  17.   lcd03.begin(9600);
  18.   lcd03.write(LCD03_HIDE_CUR);
  19.   lcd03.write(LCD03_CLEAR);
  20. }
  21. void loop(){
  22.    byte highByte, lowByte, fine;              // highByte and lowByte store high and low bytes of the bearing and fine stores decimal place of bearing
  23.    char pitch, roll;                          // Stores pitch and roll values of CMPS10, chars are used because they support signed value
  24.    int bearing;                               // Stores full bearing
  25.    
  26.    Wire.beginTransmission(ADDRESS);           //starts communication with CMPS10
  27.    Wire.write(2);                              //Sends the register we wish to start reading from
  28.    Wire.endTransmission();
  29.    Wire.requestFrom(ADDRESS, 4);              // Request 4 bytes from CMPS10
  30.    while(Wire.available() < 4);               // Wait for bytes to become available
  31.    highByte = Wire.read();           
  32.    lowByte = Wire.read();            
  33.    pitch = Wire.read();              
  34.    roll = Wire.read();               
  35.    
  36.    bearing = ((highByte<<8)+lowByte)/10;      // Calculate full bearing
  37.    fine = ((highByte<<8)+lowByte)%10;         // Calculate decimal place of bearing
  38.    
  39.    display_data(bearing, fine, pitch, roll);  // Display data to the LCD03
  40.    
  41.    delay(100);
  42. }
  43. void display_data(int b, int f, int p, int r){    // pitch and roll (p, r) are recieved as ints instead oif bytes so that they will display corectly as signed values.
  44.   
  45.   lcd03.write(LCD03_SET_CUR);                     // Set the LCD03 cursor position
  46.   lcd03.write(1);  
  47.   lcd03.print("CMPS10 Example V:");
  48.   lcd03.print(soft_ver());                        // Display software version of the CMPS10
  49.   
  50.   delay(5);                                       // Delay to allow LCD03 to proscess data  
  51.   
  52.   lcd03.write(LCD03_SET_CUR);
  53.   lcd03.write(21);  
  54.   lcd03.print("Bearing = ");                      // Display the full bearing and fine bearing seperated by a decimal poin on the LCD03
  55.   lcd03.print(b);                              
  56.   lcd03.print(".");
  57.   lcd03.print(f);
  58.   lcd03.print("  ");
  59.   delay(5);
  60.   lcd03.write(LCD03_SET_CUR);                     // Display the Pitch value to the LCD03
  61.   lcd03.write(41);
  62.   lcd03.print("Pitch = ");
  63.   lcd03.print(p);
  64.   lcd03.print(" ");
  65.   delay(5);
  66.   
  67.   lcd03.write(LCD03_SET_CUR);                     // Display the roll value to the LCD03
  68.   lcd03.write(61);
  69.   lcd03.print("Roll = ");
  70.   lcd03.print(r);
  71.   lcd03.print(" ");
  72. }
  73. int soft_ver(){
  74.    int data;                                      // Software version of  CMPS10 is read into data and then returned
  75.    
  76.    Wire.beginTransmission(ADDRESS);
  77.    // Values of 0 being sent with write need to be masked as a byte so they are not misinterpreted as NULL this is a bug in arduino 1.0
  78.    Wire.write((byte)0);                           // Sends the register we wish to start reading from
  79.    Wire.endTransmission();
  80.    Wire.requestFrom(ADDRESS, 1);                  // Request byte from CMPS10
  81.    while(Wire.available() < 1);
  82.    data = Wire.read();           
  83.    
  84.    return(data);
  85. }
复制代码

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -