I am completely new to this and I am trying to get the speed in my iOS app. I am able to get the angle but not able to get the velocity. This is the code from ESP32 which sends angle and velocity together:
// Calculate Swing Speed
if (micros() > sampleRate) {
if (startTime == 0) {
timeOfSwing = 0;
if (total_accel > 9) {
startTime = millis();
}
} else {
timeOfSwing = millis() - startTime;
if (total_accel > max_accel) {
max_accel = total_accel;
}
if (total_accel < 9) {
startTime = 0;
max_accel = 0;
}
}
sampleRate = micros() + 200;
}
velocity = max_accel * (timeOfSwing / 1000.0) * 2.237;
// Scale velocity up since accelerometer is not at tip of the bat
velocity *= 1.2;
// Calculate angle
if (sendAngle.update()) {
angle = abs(57 * atan2((double)lis.y, (double)lis.z)); // this is the angle
if (checkValue != 0) {
Serial.print(checkValue);
checkValue = 0;
} else {
// Combine angle and speed into 1 integer to send via bluetooh
data = angle << 8;
data = data | peakVelocity;
Serial.print(data);
}
}
This is the code I have written for iOS:
@State var response = Data() { didSet {
output = UInt16(String(data: response, encoding: .utf8) ?? "0") ?? 0
BatAngle = output >> 8
BatVelocity = output & 255
} }
This is the area where I want the velocity to get displayed:
Text("\(BatVelocity)")
.bold()
.font(.system(size: 52))
.foregroundColor(.black)
Please Help