-
Notifications
You must be signed in to change notification settings - Fork 1
/
Transmitter_gyro_sensor.ino
182 lines (151 loc) · 4.32 KB
/
Transmitter_gyro_sensor.ino
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Muscle sensor transmitter
// #include "SparkFunLSM6DS3.h" for gyroscope
// LSM6DS3 myIMU; //Default constructor is I2C, addr 0x6B
// myIMU.begin();
// Serial.print("\nGyroscope:\n");
// Serial.print(" X = ");
// Serial.println(myIMU.readFloatGyroX(), 4);
// Serial.print(" Y = ");
// Serial.println(myIMU.readFloatGyroY(), 4);
// Serial.print(" Z = ");
// Serial.println(myIMU.readFloatGyroZ(), 4);
// (?) suppose it goes from 0 to 180 (clockwise) and 0 to -180 (counterclockwise)
// we could read it and use to normalise the speed value for the motors
#include <printf.h>
#include <RF24_config.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
////#include "SparkFunLSM6DS3.h"
/************************** gyroscope stuff ***********************************/
#include <Wire.h>
#include <SFE_LSM9DS0.h>
// Example I2C Setup //
// Comment out this section if you're using SPI
// SDO_XM and SDO_G are both grounded, so our addresses are:
#define LSM9DS0_XM 0x1D // Would be 0x1E if SDO_XM is LOW
#define LSM9DS0_G 0x6B // Would be 0x6A if SDO_G is LOW
// Create an instance of the LSM9DS0 library called `dof` the
// params: [SPI or I2C Mode declaration],[gyro I2C address],[xm I2C add.]
LSM9DS0 dof(MODE_I2C, LSM9DS0_G, LSM9DS0_XM);
#define PRINT_CALCULATED
//#define PRINT_RAW
#define PRINT_SPEED 500 // 500 ms between prints
float x_value = 0;
/***************************** end gyroscope stuff *****************************/
RF24 radio(7, 8); // CE, CSN
////LSM6DS3 myIMU;
const byte address[6] = "00001";
int button_pin = 3;
boolean button_state = 0;
//const char text[] = " :D ";
//int readData = 0;
//int threshold = 600;
////float rotationValue = 0;
float speedValue = 0;
String msgToSend = "";
long lastTime = 0;
long interval = 2000;
float overall = 0.;
void setup() {
Serial.begin(9600);
pinMode(button_pin, INPUT);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
uint16_t status = dof.begin();
Serial.println(status, HEX);
Serial.println("Should be 0x49D4");
Serial.println();
}
void loop() {
muscleSensor();
gyroscope();
sendMsg();
delay(55);
Serial.println();
}
void sendMsg()
{
msgToSend = String(speedValue,2) + ";" + String(x_value,2);
char msg[15] = "";
msgToSend.toCharArray(msg, sizeof(msg));
Serial.print("MSG: ");
Serial.println(msg);
radio.write(&msg, sizeof(msg));
}
void muscleSensor()
{
speedValue = analogRead(A0)/ 1024.0;
speedValue = 80 + speedValue*255*1.5; // 80 is the minimum speed needed to move the car
//speedValue = 140.;
Serial.print("Speed value: ");
Serial.println(speedValue);
}
/*
void gyroscope()
{
// on wrist with pins facing the skin, yellow wire on top:
// positive value = turn right
// negative value = turn left
dof.readGyro();
int temp_value = dof.calcGyro(dof.gx);
if (abs(temp_value) > 100)
{
x_value = temp_value;
lastTime = millis();
}
else
{
if (millis() - lastTime > interval)
{
x_value = temp_value;
lastTime = millis();
}
}
Serial.print("G: ");
Serial.println(x_value, 2);
}
*/
void gyroscope()
{
// on wrist with pins facing the skin, yellow wire on top:
// positive value = turn right
// negative value = turn left
dof.readGyro();
float temp_value = dof.calcGyro(dof.gx);
if (abs(temp_value) > 20)
{
overall = overall + temp_value / 6.5f; // we devide by a float value here to make it steer less, too sensitive
}
x_value = overall;
Serial.print("G: ");
Serial.println(x_value, 2);
}
/*void printGyro()
{
// first call readGyro() function. When this exits, it'll update the
// gx, gy, and gz variables with the most current data.
dof.readGyro();
// Now we can use the gx, gy, and gz variables as we please.
// Either print them as raw ADC values, or calculated in DPS.
Serial.print("G: ");
#ifdef PRINT_CALCULATED
// on wrist with pins facing the skin, yellow wire on top:
// positive value = turn right
// negative value = turn left
x_value = dof.calcGyro(dof.gx);
Serial.println(x_value, 2);
//Serial.print(", ");
//Serial.print(dof.calcGyro(dof.gy), 2);
//Serial.print(", ");
//Serial.println(dof.calcGyro(dof.gz), 2);
#elif defined PRINT_RAW
Serial.print(dof.gx);
Serial.print(", ");
Serial.print(dof.gy);
Serial.print(", ");
Serial.println(dof.gz);
#endif
}*/