-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduinoFinalCode_wrapper.cpp
161 lines (146 loc) · 4.85 KB
/
arduinoFinalCode_wrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Include Files
*
*/
#if defined(MATLAB_MEX_FILE)
#include "tmwtypes.h"
#include "simstruc_types.h"
#else
#include "rtwtypes.h"
#endif
/* %%%-SFUNWIZ_wrapper_includes_Changes_BEGIN --- EDIT HERE TO _END */
#ifndef MATLAB_MEX_FILE
#define MPU 0x68
#define Arduino 100
#include <Arduino.h>
/*#include "includes/Wire.h"
#include "includes/Wire.cpp"
#include "includes/utility/twi.h"
#include "includes/utility/twi.c"
float RAccX,RAccY,RAccZ;
int16_t AccX,AccY,AccZ;
float AccErrorY;
*/
int eeAddress = 0; //Location we want the data to be put.
int readstate = 0; //Variable to store data read from EEPROM.
int32_t counter = 0;
int16_t perCycle = 400;
#include "includes/myLib.h"
#include "includes/myLib.cpp"
#endif
/* %%%-SFUNWIZ_wrapper_includes_Changes_END --- EDIT HERE TO _BEGIN */
#define y_width 1
/*
* Create external references here.
*
*/
/* %%%-SFUNWIZ_wrapper_externs_Changes_BEGIN --- EDIT HERE TO _END */
/* extern double func(double a); */
/* %%%-SFUNWIZ_wrapper_externs_Changes_END --- EDIT HERE TO _BEGIN */
/*
* Output function
*
*/
extern "C" void arduinoFinalCode_Outputs_wrapper(real_T *xAngle,
const real_T *xD)
{
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */
if (xD[0] == 1)
{
#ifndef MATLAB_MEX_FILE
/*
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true);
AccX = (Wire.read() << 8 | Wire.read()) ;
AccY = (Wire.read() << 8 | Wire.read()) ;
AccZ = (Wire.read() << 8 | Wire.read()) ;
RAccX = (float)AccX / 16384.0;
RAccY = (float)AccY / 16384.0;
RAccZ = (float)AccZ / 16384.0;
xAngle[0]= atan(RAccX / sqrt(RAccY *RAccY + RAccZ *RAccZ)) *180/PI;
*/
xAngle[0] = (float)counter / 400 * 360;
#endif
}
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */
}
/*
* Updates function
*
*/
extern "C" void arduinoFinalCode_Update_wrapper(real_T *xAngle,
real_T *xD)
{
/* %%%-SFUNWIZ_wrapper_Update_Changes_BEGIN --- EDIT HERE TO _END */
if (xD[0] != 1)
{
#ifndef MATLAB_MEX_FILE
pinMode(2 , INPUT_PULLUP); // Encoder A
pinMode(3 , INPUT_PULLUP); // Encoder B
pinMode(4 , INPUT_PULLUP); // Proximity Sensor
pinMode(5,OUTPUT); //In1
pinMode(6,OUTPUT); //IN2
pinMode(7,OUTPUT); //ENB
while(digitalRead(4))
{
digitalWrite(5,1);
digitalWrite(6,0);
analogWrite(7,255);
}
counter = 0;
digitalWrite(5,0);
digitalWrite(6,0);
analogWrite(7,0);
attachInterrupt(digitalPinToInterrupt(2) ,interCounter1 ,CHANGE);
attachInterrupt(digitalPinToInterrupt(3) ,interCounter2 ,CHANGE);
/*
Wire.begin(); // Initialize comunication
Wire.setClock(400000);
Wire.beginTransmission(MPU); // Start communication with MPU6050 // MPU=0x68
Wire.write(0x6B); // Talk to the register 6B
Wire.write(0x00); // Make reset - place a 0 into the 6B register
Wire.endTransmission(true); //end the transmission
// Set Accel On 2 g
Wire.beginTransmission(MPU);
Wire.write(0x1C); // sends instruction byte
Wire.write(0b00000000); // sends potentiometer value byte
Wire.endTransmission(); // stop transmitting
// Set Sample Rate on 1kHz and 100 Hz low pass
Wire.beginTransmission(MPU);
Wire.write(0x1A); // sends instruction byte
Wire.write(0b00000100); // sends potentiometer value byte
Wire.endTransmission(); // stop transmitting
// Activate Sample Rate divider on ? ~ 255 Hz sampling Don't ask how
Wire.beginTransmission(MPU);
Wire.write(0x19); // sends instruction byte
Wire.write(0x07); // sends potentiometer value byte
Wire.endTransmission(); // stop transmitting
// Activate the Interrupt pin
Wire.beginTransmission(MPU);
Wire.write(0x38); // sends instruction byte
Wire.write(0b00000001); // sends potentiometer value byte
Wire.endTransmission(); // stop transmitting
for(int i = 0 ; i < 200 ; i++) {
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true);
AccX = (Wire.read() << 8 | Wire.read()) ;
AccY = (Wire.read() << 8 | Wire.read()) ;
AccZ = (Wire.read() << 8 | Wire.read()) ;
RAccX = (float)AccX / 16384.0;
RAccY = (float)AccY / 16384.0;
RAccZ = (float) AccZ / 16384.0;
// Sum all readings
AccErrorY = AccErrorY + ((atan(-1 * (RAccX) / sqrt(pow((RAccY), 2) + pow((RAccZ), 2))) * 180 / PI));
}
AccErrorY = AccErrorY / 200;
delay(10);
*/
#endif
xD[0] = 1;
}
/* %%%-SFUNWIZ_wrapper_Update_Changes_END --- EDIT HERE TO _BEGIN */
}