Learning Processing with Arduino

From Wiki

Jump to: navigation, search

Kit List

Introduction:
Arduino advanced kit provided by ELECFREAKS is based on our starter kit to expand more and more popular modules owing the characteristics of plug and use without any requirement of soldering. We have well prepared six courses for learners to learn the usage of modules in an intersting environment which is better for stimulating your thinking mind. For example,Tetris games help us to study the usage of the LCD screen and Arduino program. Color picker used can help us to learn the color sensor as well as BLE Bluetooth communication and Android interface development ... Practical Course included LCD screen, sensor, MP3, BLE, WIFI, Android development, and image interaction (processing), make you open-minded!

Part5 Learning Processing with Arduino

Product introduction:
AD-part-5.gif It is connected to a 9-axis sensor on the Arduino board to control Processing programs' running on the computer.
Processing1.png Processing2.png
Materials:
Uno board connected to a 9DOF (9-axis sensor), and then connect to the PC via usb cable
*1xFreaduino Uno;
*1x9DOF MODULE V1.2;

Parameter and IO definition:
1.5V power supply to mainboard
2.9DOF access to board #define SC A5 // Please notice the Pin's define,A(Analog pin) #define SD A4 #define V V(power supply) #define G G(ground)
Notes
A.Development Tools of Processing Processing2.2.1(above 2.2) need to be downloaded and installed to run processing program on pc end(in part5.zip)
And this program needs to be connected to one of Arduino ends via USB to get opened, or it can't identify Arduino board. Processing learnig please refer to Chiese website http://www.hiprocessing.net,English website http://www.openprocessing.org/browse/
B.Sensors must be placed horizontally and with terminals toward the right-hand side.
C.To run processing.pde must unzip the entire processing folder, and then load the resource file of the folder to run. The following is the example figure of running.
Processing.png
Working principles:
Arduino board reads the stance sensor data, and pass through the serial port to the computer, then processing will automatically connect serial ports occupied by Arduino board
to receive the data, so that control program running.

Arduino代码

part5.zip


#include <Wire.h>
/************ 传感器参数 ***********/
#define Acc (0x53)          // ADXL345地址
#define Gyr (0x68)           // L3205D地址
#define Mag 0x1E           // HMC5883L地址

#define G_SMPLRT_DIV 0x15
#define G_DLPF_FS 0x16
#define G_INT_CFG 0x17
#define G_PWR_MGM 0x3E
// 加速度传感器误差修正的偏移量
 
int a_offx = -30;
 
int a_offy = -8;
int a_offz = 0;
// 陀螺仪误差修正的偏移量
 
int g_offx = 67;
int g_offy = 5;
int g_offz = 41;
 
#define Gry_offset -13     // 陀螺仪偏移量
#define Gyr_Gain 0.07      // 满量程2000dps时灵敏度(dps/digital)
#define pi 3.14159
void writeRegister(int device, byte address, byte val);
void sensor_init();
int gDat(int device, int axis);
void readFrom(int DEVICE, byte address, int num, byte buff[]);
void getAccelerometerData(int * result);
/********** 互补滤波器参数 *********/
unsigned long preTime = 0; // 采样时间
float f_angleX = 0.0;  // 滤波处理后的角度值
float f_angleY = 0.0;  // 滤波处理后的角度值
float f_angleZ = 0.0;  // 滤波处理后的角度值
 
 
/*********** PID控制器参数 *********/
unsigned long lastTime;    // 前次时间
float ITerm, lastInput;    // 积分项、前次输入
float Output = 0.0;        // PID输出值
 
/************ 程序初始化 ***********/
void setup() {
	Wire.begin();
    sensor_init();        // 配置传感器
    Serial.begin(19200);  // 开启串口以便监视数据
 
    delay(1000);
  }
/************** 主程序 *************/
int acc[3];
int gyro[4];
void loop() {
    unsigned long now = millis();                            // 当前时间(ms)
    float dt = (now - preTime) / 1000.0;                     // 微分时间(s)
    int time=now-preTime;
    preTime = now;                                           // 记录本次时间(ms)
 
/********** 读取姿态传感器 *********/
 
    // float Y_Acc = gDat(Acc, 1);                             // 获取向前的加速度(digite)
    getAccelerometerData(acc);
    getAccelerometerData(gyro);
    float X_Acc = acc[0];                             // 获取向前的加速度(digite)
    float Y_Acc = acc[1];                             // 获取向前的加速度(digite)
    float Z_Acc = acc[2];                             // 获取向下的加速度(digite)
    float angleX = atan(Y_Acc / Z_Acc) * 180 / pi;   // 根据加速度分量得到的角度(degree)
    float angleY = atan(X_Acc/Z_Acc  ) * 180 / pi;   // 根据加速度分量得到的角度(degree)
    float angleZ = atan(X_Acc / Y_Acc) * 180 / pi;   // 根据加速度分量得到的角度(degree)
   // float omega =  Gyr_Gain * (gDat(Gyr, 0) +  Gry_offset); // 当前角速度(degree/s)
 
    float omegaX =  Gyr_Gain * (gyro[0] +  Gry_offset); // 当前角速度(degree/s)
    float omegaY =  Gyr_Gain * (gyro[1] +  Gry_offset); // 当前角速度(degree/s)
    float omegaZ =  Gyr_Gain * (gyro[2] +  Gry_offset); // 当前角速度(degree/s)
 
/*********** 一阶互补滤波 **********/
    float K = 0.8;                                          // 取值权重
    float A = K / (K + dt);                                 // 加权系数
    f_angleX =(A *(f_angleX + omegaX * dt) + (1-A) * angleX);  // 互补滤波算法
    f_angleY =(A *(f_angleY + omegaY * dt) + (1-A) * angleY);  // 互补滤波算法
    f_angleZ =(A *(f_angleZ + omegaZ * dt) + (1-A) * angleZ);  // 互补滤波算法
 
 
 
 
   Serial.print(",");
   Serial.print("X=");   // 偏离角度
   Serial.print(",");
   Serial.print(f_angleX, 2);   // 偏离角度
   Serial.print(",");
   Serial.print("Y=");   // 偏离角度
   Serial.print(",");
   Serial.print(f_angleY, 2);   // 偏离角度
   Serial.println(",");
   delay(10);
}
 
/***************************************
九轴姿态传感器寄存器读取函数
For Arduino,
****************************************
        调用参数表
****************************************
  type    device      axis
                   0   1   2
ADXL345     Acc    x   y   z
L3G4200D    Gyr    x   y   z
HMC5883L    Mag    x   z   y
****************************************
Example
****************************************
00 #include <Wire.h>
01 #define Acc 0x1D;
02 #define Gyr 0x69;
03 #define Mag 0x1E;
04
05  void setup() {
06    sensor_init();
07    delay(1000);
08  }
09
10  void loop() {
11    int Z-Gyroscope;
12    Z-Gyroscope = gDat(Gyr, 2);
13    delay(50);
14  }
***************************************/
 
int gDat(int device, int axis) {
    int v;
    byte vL, vH, address;               // 存放byte数值
    if (device == Acc) address = 0x32;  // ADXL345的读数地址
    if (device == Gyr) address = 0x1B;  // L3G3200D的读数地址
    if (device == Mag) address = 0x03;  // HMC5883L的读数地址
    address = address + axis * 2;       // 数据偏移-坐标轴
    Wire.beginTransmission(device);     // 开始传输数据
    Wire.write(address);                 // 发送指针
    Wire.endTransmission();
    Wire.beginTransmission(device);     // 开始传输数据
    Wire.requestFrom(device, 2);        // 请求2 byte数据
    if(Wire.available())vL = Wire.read();       // 成功获取前等待
    if(Wire.available())vH = Wire.read();      // 读取数据
    Wire.endTransmission();             // 结束传输
    if (device == Mag) v = (vL << 8) | vH;
    else v = (vH << 8) | vL;            // 将byte数据合并为Int
    return v;                           // 返回读书值
}
//得到 加速度的数据
void getAccelerometerData(int * result) {
 
  int regAddress = 0x32;    //加速度传感器ADXL345第一轴的数据的设定
 
  byte buff[6];
 
  readFrom(Acc, regAddress,6, buff); //读取加速度传感器ADXL345的数据
 
  //每个轴的读数有10位分辨率,即2个字节.
 
  //我们要转换两个bytes为一个int变量
 
 result[0] = (((int)buff[1]) << 8) | buff[0] + a_offx;
 result[1] = (((int)buff[3]) << 8) | buff[2] + a_offy;
 result[2] = (((int)buff[5]) << 8) | buff[4] + a_offz;
 
}
//得到陀螺仪的值
void getGyroscopeData(int * result)
 
{
 
 
 
  int regAddress = 0x1B;
 
  int temp, x, y, z;
 
  byte buff[8];
 
  readFrom(Gyr, regAddress,8, buff); //读取陀螺仪ITG3200的数据
 
  result[0] = ((buff[2] << 8) | buff[3]) + g_offx;
 
  result[1] = ((buff[4] << 8) | buff[5]) + g_offy;
 
  result[2] = ((buff[6] << 8) | buff[7]) + g_offz;
 
  result[3] = (buff[0] << 8) | buff[1]; // 温度
 
}
 
 
//读IIC数据
void readFrom(int DEVICE, byte address, int num, byte buff[]) {
 
  Wire.beginTransmission(DEVICE); //开始传送至加速度传感器
 
  Wire.write(address);        //发送读取的地址
 
  Wire.endTransmission(); //结束传输
 
  Wire.beginTransmission(DEVICE); //开始传送到ACC
 
  Wire.requestFrom(DEVICE, num);    // 要求从加速度传感器中发送6个字节的数据
 
  int i = 0;
 
  while(Wire.available())    //当加速度传感器返回的数据小于要求值时(异常情况)
 
  {
 
    buff[i] = Wire.read(); // 接收数据
 
    i++;
 
  }
 
  Wire.endTransmission(); //结束传输
 
}
/********************************************
配置九轴姿态传感器
********************************************/
void sensor_init() {                         // 配置九轴姿态传感器
    //writeRegister(Acc, 0x2D, 0b00001000);    // 测量模式
                            // 配置ADXL345
    //调用 ADXL345
	writeRegister(Acc, 0x2D, 0);
    writeRegister(Acc, 0x2D, 16);
    writeRegister(Acc, 0x2D, 8);
 
//设定在 +-2g 时的默认读数
 
 
//    writeRegister(Gyr, 0x20, 0b00001111);    // 设置睡眠模式、x, y, z轴使能
//    writeRegister(Gyr, 0x21, 0b00000000);    // 选择高通滤波模式和高通截止频率
//    writeRegister(Gyr, 0x22, 0b00000000);    // 设置中断模式
//    writeRegister(Gyr, 0x23, 0b00110000);    // 设置量程(2000dps)、自检状态、SPI模式
//    writeRegister(Gyr, 0x24, 0b00000000);    // FIFO & 高通滤波
//                            // 配置L3G4200D(2000 deg/sec)
 
    writeRegister(Gyr, G_PWR_MGM, 0x00);
    writeRegister(Gyr, G_SMPLRT_DIV, 0x07); // EB, 50, 80, 7F, DE, 23, 20, FF
    writeRegister(Gyr, G_DLPF_FS, 0x1E);    // +/- 2000 dgrs/sec, 1KHz, 1E, 19
    writeRegister(Gyr, G_INT_CFG, 0x00);
 
   // writeRegister(Mag, 0x02, 0x00);          // 连续测量
                            // 配置HMC5883L
 
}
/********************************************
寄存器写入函数
********************************************/
void writeRegister(int device, byte address, byte val) {
    Wire.beginTransmission(device);           // 写入的传感器
    Wire.write(address);                      // 写入地址
    Wire.write(val);                          // 写入值
    Wire.endTransmission();                   // 结束传输
}

Personal tools
Namespaces
Variants
Actions
Navigation
Support
Toolbox