matlab arduino serial communicatrion

3 Ansichten (letzte 30 Tage)
Kabamba Muteba
Kabamba Muteba am 28 Mai 2020
arduino code
#include <Wire.h>
#include <MPU6050_tockn.h>
MPU6050 mpu6050(Wire);
long timer = 0;
#define ENC_A1 2
#define ENC_B1 3
#define ENC_A2 18
#define ENC_B2 19
// Main loop refresh period.
#define REFRESH_MS 50
// Main serial data connection to computer.
#define BAUD_RATE 9600
// Encoder signal line states
volatile boolean state_a1 = 0;
volatile boolean state_b1 = 0;
// Encoder signal line states
volatile boolean state_a2 = 0;
volatile boolean state_b2 = 0;
// Encoder position
volatile int enc_pos_1 = 0;
int enc_pos_prev_1 = 0;
int enc_pos_change_1 = 0;
volatile int enc_pos_2 = 0;
int enc_pos_prev_2 = 0;
int enc_pos_change_2 = 0;
// normally on a differential drive system to when moving
// forwards the wheels are rotating in opposite directions
int encoder_direction_left = 1;
int encoder_direction_right = 1;
// Timing
unsigned long micros_current = 0;
unsigned long micros_prev = 0;
long micros_change = 0;
int dleft = 0;
int dright = 0;
float pi = 3.142;
float TWOPI = 6.284;
float diameter = 42; //(mm)
int count_per_revolution = 12; //(mm)
int Lwheel_c = 140; //(mm)
int Rwheel_c = 139.9; //(mm)
float Rright = 0.00; // radius of curvature of right wheel
float Rleft = 0.00; //Radius of curvature of left wheel
float wheel_Rc = 0.00 ; //Radius of curvature
float wheel_theta = 0.00;
float x_pos= 0.00;
float y_pos = 0.00;
int wheelbase = 88; // wheelbase
void setup() {
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
pinMode(ENC_A1, INPUT);
pinMode(ENC_B1, INPUT);
pinMode(ENC_A2, INPUT);
pinMode(ENC_B2, INPUT);
state_a1 = (boolean) digitalRead(ENC_A1);
state_b1 = (boolean) digitalRead(ENC_B1);
state_a2 = (boolean) digitalRead(ENC_A2);
state_b2 = (boolean) digitalRead(ENC_B2);
attachInterrupt(0, interrupt_enc_a1, CHANGE);
attachInterrupt(1, interrupt_enc_b1, CHANGE);
attachInterrupt(5, interrupt_enc_a2, CHANGE);
attachInterrupt(4, interrupt_enc_b2, CHANGE);
micros_prev = micros();
Serial.begin(BAUD_RATE);
}
void loop() {
mpu6050.update();
if(millis() - timer > 1000){
timer = millis();
}
// Calculate elapsed time
micros_current = micros();
if (micros_current < micros_prev) {
micros_change = micros_current + (4294967295 - micros_prev);
} else {
micros_change = micros_current - micros_prev;
}
// determine how many ticks since our last sampling?
// Calculate change in encoder1 position.
enc_pos_change_1 = enc_pos_1 - enc_pos_prev_1;
//enc_pos_change_1 = abs(enc_pos_change_1);
// Calculate change in encoder2 position.
enc_pos_change_2 = enc_pos_2 - enc_pos_prev_2;
//enc_pos_change_2 = abs(enc_pos_change_2);
// Emit data
Serial.print(enc_pos_1);
Serial.print("\t");
Serial.print(enc_pos_2);
Serial.print("\t");
Serial.print(enc_pos_change_1);
Serial.print("\t");
Serial.print(enc_pos_change_2);
Serial.print("\t");
//Serial.print(dright);
//Serial.print("\t");
//Serial.print(dleft);
//Serial.print("\t");
Serial.print(x_pos);
Serial.print("\t");
Serial.print(y_pos);
Serial.print("\t");
Serial.print(wheel_theta);
Serial.print("\t");
Serial.print("angleX : ");Serial.print(mpu6050.getAngleX());
Serial.print("\tangleY : ");Serial.print(mpu6050.getAngleY());
Serial.print("\tangleZ : ");Serial.println(mpu6050.getAngleZ());
// and update last sampling for next time
enc_pos_prev_1 = enc_pos_1;
enc_pos_prev_2 = enc_pos_2;
micros_prev = micros_current;
// convert longs to floats and ticks to mm
//dleft = (enc_pos_change_1) * Lwheel_c/count_per_revolution;
//dright = (enc_pos_change_2)* Rwheel_c/count_per_revolution;
dleft = (enc_pos_change_1) * Lwheel_c;
dright = (enc_pos_change_2)* Rwheel_c;
// constrain _theta to the range 0 to 2 pi
//if (theta > 2.0 * pi)
// theta -= 2.0 * pi;
// else if (theta < -2.0*pi)
// theta += 2.0 * pi;
// calculate distance we have traveled since last sampling
wheel_Rc = (dleft + dright)/2;
// accumulate total rotation around our center
wheel_theta += (dleft + dright)/wheelbase;
//and clip the rotation to plus or minus 360 degrees
wheel_theta -= (float)((int)(wheel_theta/TWOPI))*TWOPI;
if (wheel_theta < -PI) { wheel_theta += TWOPI; }
else { if (wheel_theta > PI) wheel_theta -= TWOPI; }
x_pos = x_pos + wheel_Rc * cos(wheel_theta);
y_pos = y_pos + wheel_Rc * sin(wheel_theta);
delay(REFRESH_MS);
}
// Detect pulses from depth encoder.
void interrupt_enc_a1() {
if (!state_a1) {
state_b1 ? enc_pos_1++: enc_pos_1--;
}
state_a1 = !state_a1;
}
void interrupt_enc_b1() {
state_b1 = !state_b1;
}
// Detect pulses from depth encoder.
void interrupt_enc_a2() {
if (!state_a2) {
state_b2 ? enc_pos_2++: enc_pos_2--;
}
state_a2 = !state_a2;
}
void interrupt_enc_b2() {
state_b2 = !state_b2;
}
matlab code below
% Initializing I2C devices...
% Testing device connections...
% MPU6050 connection failed
% Send any character to begin DMP programming and demo:
% Initializing DMP...
% Enabling DMP...
% Enabling interrupt detection (Arduino external interrupt 0)...
% DMP ready! Waiting for first interrupt...
%while (1)
delete(instrfindall);
warning off
s = serial('COM3','Baudrate',9600,'DataBits',8, 'Terminator','CR/LF');
fclose(s);
warning off;
% warning('off','MATLAB:serial:fscanf:unsuccesfulRead');
fopen(s);
%fclose(s);
tic
while(toc<2)
data = fscanf(s, '%s') %,'X=%f, Y=%f, Yaw=%f')
end
fprintf(s,'a1231');
while(toc<600)
data = fscanf(s, '%s') %,'X=%f, Y=%f, Yaw=%f')
end
matlab output
data =
'00000.000.000.00angleX:6.00angleY:3.24angleZ:-0.47'
data =
'00000.000.000.00angleX:6.00angleY:3.29angleZ:-0.47'
data =
'00000.000.000.00angleX:6.00angleY:3.26angleZ:-0.48'
arduino output which is supposed to be matlab output aswell
0 0 0 0 0.00 0.00 0.00 angleX : 6.76 angleY : 2.74 angleZ : 0.41
0 0 0 0 0.00 0.00 0.00 angleX : 6.74 angleY : 2.78 angleZ : 0.43
0 0 0 0 0.00 0.00 0.00 angleX : 6.71 angleY : 2.76 angleZ : 0.43
0 0 0 0 0.00 0.00 0.00 angleX : 6.72 angleY : 2.77 angleZ : 0.43
The result obtained from matlab from angglex is not the true reflection of what am expected or what i have from my arduino code output. pls can someone help me find out what the problem is one my codes.

Antworten (0)

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange

Produkte


Version

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by