forked from trlafleur/Fastnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
B&G_Wind_Test.c
76 lines (60 loc) · 2.78 KB
/
B&G_Wind_Test.c
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
// Converts B&G 213 MHU to degrees...
// The MHU uses a 3-phase synchro, so we have three values that are 120 degrees apart
// B&G unit runs on 6.5vdc, so we need to scale to 3.3v with a 2/1 divider
// We will see a slight error as we sample ATD in sequence, but, it's just not a real issue
/*
CHANGE LOG:
DATE REV DESCRIPTION
----------- --- ----------------------------------------------------------
07-Jun-2012 1.0 TRL - First Build
19-Feb-2024 2.0 TRL - Code clean up, update algorithm, added references
02-Mar-2024 2.0a TRL - added comments, removed un-needed variable
Notes: 1) Tested with https://www.onlinegdb.com/online_c_compiler
2) Function tested with ESP32 with external ATD converter
3) Tested with SAMD21G18 on Arduino
*/
// https://www.hisse-et-oh.com/store/medias/sailing/5dd/2c3/af3/original/5dd2c3af39819f3668e93b13.pdf
// https://electronics.stackexchange.com/questions/160215/how-to-get-the-angle-from-a-3-phases-synchro-signal
// https://bikerglen.com/blog/building-a-synchro-to-digital-converter/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// ************************************************ //
// 213 B&G x = Red, y = Green, z = Blue
// B&G MHU is offset by 270 deg
void getAngle( float x, float y, float z)
{
float rad_angle = 0.0;
float deg_angle = 0.0;
int int_angle = 0;
float M_Sin = 0.0;
float M_Cos = 0.0;
// Compute a Scott Transform
M_Sin = (z-x);
M_Cos = (y - ( (x+z) / 2)) * ( 2 / sqrt(3) ); // (2 / sqrt(3)) = 1.15470054
rad_angle = atan2 (M_Cos, M_Sin); // get angle
deg_angle = (rad_angle * (180.0 / 3.14159265)); // (180.0 / 3.14159265) = 57.29577958
deg_angle = (deg_angle + 270.0); // B&G Offset
if (deg_angle >= 360) deg_angle = deg_angle - 360; // check for max angle
int_angle = (int) round (deg_angle) % 360; // convert to int
printf("Angle in degree = %8.2f , %d \n", deg_angle, int_angle);
}
// Test program for 213 MHU based on voltage values from B&G
// ************************************************ //
int main()
{
printf("B&G MHU Test...\n");
getAngle(0.19, 4.74, 4.74); // 0
getAngle(0.53, 3.24, 5.89); // 30
getAngle(1.64, 1.68, 6.33); // 60
getAngle(3.25, 0.47, 5.93); // 90
getAngle(4.77, 0.10, 4.78); // 120
getAngle(5.87, 0.53, 3.25); // 150
getAngle(6.28, 1.67, 1.69); // 180
getAngle(5.87, 3.25, 0.53); // 210
getAngle(4.77, 4.78, 0.10); // 240
getAngle(3.25, 5.93, 0.47); // 270
getAngle(1.64, 6.33, 1.68); // 300
getAngle(0.53, 5.89, 3.24); // 330
return 0;
}