Browse Source

accelerometer and display fix

Yohan Simard 3 years ago
parent
commit
64ea0cbd78
10 changed files with 54 additions and 87 deletions
  1. 6
    63
      MDK-ARM/Project.uvoptx
  2. 8
    4
      MyDrivers/Timer.c
  3. 1
    1
      MyDrivers/Timer.h
  4. 8
    4
      Services/Accelerometer.c
  5. 6
    1
      Services/ServoMotor.c
  6. 14
    8
      Src/Display.c
  7. 1
    1
      Src/Orientation.c
  8. 8
    3
      Src/Roll.c
  9. 1
    1
      Src/Sail.c
  10. 1
    1
      Src/main.c

+ 6
- 63
MDK-ARM/Project.uvoptx View File

@@ -75,7 +75,7 @@
75 75
       <OPTFL>
76 76
         <tvExp>1</tvExp>
77 77
         <tvExpOptDlg>0</tvExpOptDlg>
78
-        <IsCurrentTarget>0</IsCurrentTarget>
78
+        <IsCurrentTarget>1</IsCurrentTarget>
79 79
       </OPTFL>
80 80
       <CpuCode>18</CpuCode>
81 81
       <DebugOpt>
@@ -130,7 +130,7 @@
130 130
         <SetRegEntry>
131 131
           <Number>0</Number>
132 132
           <Key>DLGTARM</Key>
133
-          <Name>(1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=204,363,702,652,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=1045,583,1603,1080,0)(121=971,583,1529,1080,0)(122=-1,-1,-1,-1,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=200,173,988,1026,0)(131=405,227,1193,1080,0)(132=161,47,949,900,0)(133=1013,172,1801,1025,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=320,247,1120,1026,0)(151=1120,301,1920,1080,0)</Name>
133
+          <Name>(1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=204,363,702,652,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=293,271,851,768,0)(121=854,376,1412,873,0)(122=-1,-1,-1,-1,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=200,173,988,1026,0)(131=410,141,1198,994,0)(132=161,47,949,900,0)(133=966,188,1754,1041,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=320,247,1120,1026,0)(151=1120,301,1920,1080,0)</Name>
134 134
         </SetRegEntry>
135 135
         <SetRegEntry>
136 136
           <Number>0</Number>
@@ -157,41 +157,9 @@
157 157
         <Bp>
158 158
           <Number>0</Number>
159 159
           <Type>0</Type>
160
-          <LineNumber>41</LineNumber>
161
-          <EnabledFlag>0</EnabledFlag>
162
-          <Address>134220664</Address>
163
-          <ByteObject>0</ByteObject>
164
-          <HtxType>0</HtxType>
165
-          <ManyObjects>0</ManyObjects>
166
-          <SizeOfObject>0</SizeOfObject>
167
-          <BreakByAccess>0</BreakByAccess>
168
-          <BreakIfRCount>1</BreakIfRCount>
169
-          <Filename>..\Services\Accelerometer.c</Filename>
170
-          <ExecCommand></ExecCommand>
171
-          <Expression>\\NUCLEO_F103RB\../Services/Accelerometer.c\41</Expression>
172
-        </Bp>
173
-        <Bp>
174
-          <Number>1</Number>
175
-          <Type>0</Type>
176 160
           <LineNumber>33</LineNumber>
177 161
           <EnabledFlag>0</EnabledFlag>
178
-          <Address>134231164</Address>
179
-          <ByteObject>0</ByteObject>
180
-          <HtxType>0</HtxType>
181
-          <ManyObjects>0</ManyObjects>
182
-          <SizeOfObject>0</SizeOfObject>
183
-          <BreakByAccess>0</BreakByAccess>
184
-          <BreakIfRCount>1</BreakIfRCount>
185
-          <Filename>..\Services\Accelerometer.c</Filename>
186
-          <ExecCommand></ExecCommand>
187
-          <Expression>\\NUCLEO_F103RB\../Services/Accelerometer.c\33</Expression>
188
-        </Bp>
189
-        <Bp>
190
-          <Number>2</Number>
191
-          <Type>0</Type>
192
-          <LineNumber>33</LineNumber>
193
-          <EnabledFlag>0</EnabledFlag>
194
-          <Address>134231194</Address>
162
+          <Address>134231986</Address>
195 163
           <ByteObject>0</ByteObject>
196 164
           <HtxType>0</HtxType>
197 165
           <ManyObjects>0</ManyObjects>
@@ -207,37 +175,12 @@
207 175
         <Ww>
208 176
           <count>0</count>
209 177
           <WinNumber>1</WinNumber>
210
-          <ItemText>windAngle,0x0A</ItemText>
178
+          <ItemText>sailAngle,0x0A</ItemText>
211 179
         </Ww>
212 180
         <Ww>
213 181
           <count>1</count>
214 182
           <WinNumber>1</WinNumber>
215
-          <ItemText>angleDeg,0x0A</ItemText>
216
-        </Ww>
217
-        <Ww>
218
-          <count>2</count>
219
-          <WinNumber>1</WinNumber>
220
-          <ItemText>volts</ItemText>
221
-        </Ww>
222
-        <Ww>
223
-          <count>3</count>
224
-          <WinNumber>1</WinNumber>
225
-          <ItemText>angleRad</ItemText>
226
-        </Ww>
227
-        <Ww>
228
-          <count>4</count>
229
-          <WinNumber>1</WinNumber>
230
-          <ItemText>value,0x0A</ItemText>
231
-        </Ww>
232
-        <Ww>
233
-          <count>5</count>
234
-          <WinNumber>1</WinNumber>
235
-          <ItemText>speed,0x0A</ItemText>
236
-        </Ww>
237
-        <Ww>
238
-          <count>6</count>
239
-          <WinNumber>1</WinNumber>
240
-          <ItemText>TIMx-&gt;CCR2</ItemText>
183
+          <ItemText>dutyCycle</ItemText>
241 184
         </Ww>
242 185
       </WatchWindow1>
243 186
       <Tracepoint>
@@ -352,7 +295,7 @@
352 295
       <OPTFL>
353 296
         <tvExp>1</tvExp>
354 297
         <tvExpOptDlg>0</tvExpOptDlg>
355
-        <IsCurrentTarget>1</IsCurrentTarget>
298
+        <IsCurrentTarget>0</IsCurrentTarget>
356 299
       </OPTFL>
357 300
       <CpuCode>18</CpuCode>
358 301
       <DebugOpt>

+ 8
- 4
MyDrivers/Timer.c View File

@@ -229,10 +229,14 @@ float Timer_pwmo_getDutyCycle(TIM_TypeDef * timer, int channel)
229 229
 {
230 230
 	int compare = 0;
231 231
 	const int arr = LL_TIM_GetAutoReload(timer);
232
-	if (channel == LL_TIM_CHANNEL_CH1) compare = LL_TIM_OC_GetCompareCH1(timer);
233
-	else if (channel == LL_TIM_CHANNEL_CH2) compare = LL_TIM_OC_GetCompareCH2(timer);
234
-	else if (channel == LL_TIM_CHANNEL_CH3) compare = LL_TIM_OC_GetCompareCH3(timer);
235
-	else  compare = LL_TIM_OC_GetCompareCH4(timer);
232
+	if (channel == LL_TIM_CHANNEL_CH1) 
233
+		compare = LL_TIM_OC_GetCompareCH1(timer);
234
+	else if (channel == LL_TIM_CHANNEL_CH2) 
235
+		compare = LL_TIM_OC_GetCompareCH2(timer);
236
+	else if (channel == LL_TIM_CHANNEL_CH3) 
237
+		compare = LL_TIM_OC_GetCompareCH3(timer);
238
+	else  
239
+		compare = LL_TIM_OC_GetCompareCH4(timer);
236 240
 	return ((float) compare) / ((float) arr);
237 241
 }
238 242
 

+ 1
- 1
MyDrivers/Timer.h View File

@@ -79,7 +79,7 @@ void Timer_conf(TIM_TypeDef * timer, int arr, int psc);
79 79
 	* @brief  Configure le timer en mode PWM input
80 80
   * @note
81 81
 	* @param  TIM_TypeDef Timer : indique le timer à utiliser : TIM1, TIM2, TIM3 ou TIM4
82
-	*					int channel 			: Le channel utilisé par la PWM
82
+	*					int channel 			: Le channel utilisé par la PWM (pas LL)
83 83
   * @retval None
84 84
   */
85 85
 void Timer_pwmi_conf(TIM_TypeDef * timer, int channel);

+ 8
- 4
Services/Accelerometer.c View File

@@ -1,6 +1,7 @@
1 1
 #include "Accelerometer.h"
2 2
 #include "ADC.h"
3 3
 #include "math.h"
4
+#include "stdio.h"
4 5
 
5 6
 #define M_PI 3.14159265358979323846
6 7
 
@@ -8,8 +9,10 @@
8 9
 // Donc sensibilité de +- 480 mv/g
9 10
 
10 11
 
11
-const float ZERO_G = 1.65; 	// 0 g
12
-const float SENSITIVITY = 0.48;
12
+const float ZERO_G = 1.27; 	// 0 g
13
+const float SENSITIVITY = 1.35-0.9;
14
+// max: 1.75
15
+// min: 0.935
13 16
 
14 17
 void Accelerometer_conf(ADC_TypeDef *adc, GPIO_TypeDef * gpio, int pinx, int piny)
15 18
 {
@@ -35,10 +38,11 @@ float voltsToG(float volts)
35 38
 
36 39
 int Accelerometer_getAngle(ADC_TypeDef *adc, int channel)
37 40
 {
38
-	const float readG = voltsToG(ADC_readVolt(adc, channel));
41
+	const float readV = ADC_readVolt(adc, channel);
42
+	const float readG = voltsToG(readV);
39 43
 	
40 44
 	float angleRad = asin(readG);
41 45
 	int angleDeg = angleRad * (180/M_PI);
42
-	
46
+		
43 47
 	return angleDeg;
44 48
 }

+ 6
- 1
Services/ServoMotor.c View File

@@ -18,6 +18,11 @@ float convertAngleToDutyCycle(int angle) {
18 18
 	return (float)angle / 1800.0 + 0.05;
19 19
 }
20 20
 
21
+int convertDutyCycleToAngle(float dutyCycle) {
22
+	// fonction lineaire [0.049 - 0.1] -> [0 - 90]
23
+	return (dutyCycle - 0.049) * 1800;
24
+}
25
+
21 26
 void ServoMotor_setAngle(TIM_TypeDef * timer, int channel, int angle)
22 27
 {
23 28
 	Timer_pwmo_setDutyCycle(timer, channel, convertAngleToDutyCycle(angle));
@@ -26,5 +31,5 @@ void ServoMotor_setAngle(TIM_TypeDef * timer, int channel, int angle)
26 31
 int ServoMotor_getAngle(TIM_TypeDef * timer, int channel)
27 32
 {
28 33
 	const float dutyCycle = Timer_pwmo_getDutyCycle(timer, channel);
29
-	return 359 * dutyCycle;
34
+	return convertDutyCycleToAngle(dutyCycle);
30 35
 }

+ 14
- 8
Src/Display.c View File

@@ -28,7 +28,6 @@ void Display_start()
28 28
 	RFEmitter_start(EMITTER_USART);
29 29
 }
30 30
 
31
-
32 31
 void Display_background(int secCounter)
33 32
 {
34 33
 	static int previousEmergencyState = 0;
@@ -36,20 +35,27 @@ void Display_background(int secCounter)
36 35
 	
37 36
 	const int emergency = Sail_getEmergencyState();
38 37
 	
39
-	if (secCounter - prevSendSec == 3 || (emergency && !previousEmergencyState)) {
38
+	if (secCounter - prevSendSec == 1 || (emergency && !previousEmergencyState)) {
39
+		previousEmergencyState = emergency;
40 40
 		prevSendSec = secCounter;
41 41
 		
42 42
 		const float voltage = Voltage_getVoltage(VOLTAGE_ADC, VOLTAGE_CHANNEL);
43 43
 		const int sailAngle = Sail_getSailAngle();
44
-		const int relativeAngle = sailAngle >= 180 ? 360 - sailAngle : sailAngle;
45
-		const int sailPercent = (90 - relativeAngle) * 100 / 90;
44
+		const int sailPercent = sailAngle * 100 / 90.0;
45
+//		const int relativeAngle = sailAngle >= 180 ? 360 - sailAngle : sailAngle;
46
+//		const int sailPercent = (90 - relativeAngle) * 100 / 90;
47
+		
46 48
 		
47
-		char displayStr[100];
48 49
 		
49
-		sprintf(displayStr, "Bordage actuel = %d%%\nTension de la batterie = %fV\n", sailPercent, voltage);
50
-		if (emergency)
51
-			sprintf(displayStr, "Limite de roulis atteinte !\n%s", displayStr);
50
+		char displayStr[100] = "";
51
+		char emergStr[30] = "";
52 52
 		
53
+		if (emergency) {
54
+			sprintf(emergStr, "Limite de roulis atteinte !\n\r");
55
+			RFEmitter_send(EMITTER_USART, emergStr, strlen(emergStr), EMITTER_GPIO, EMITTER_PIN);
56
+		}
57
+
58
+		sprintf(displayStr, "Angle de la voile : %ddeg\n\rBordage actuel = %d%% \n\rTension de la batterie = %fV \n\r\n\r", sailAngle, sailPercent, voltage);
53 59
 		RFEmitter_send(EMITTER_USART, displayStr, strlen(displayStr), EMITTER_GPIO, EMITTER_PIN);
54 60
 	}
55 61
 	

+ 1
- 1
Src/Orientation.c View File

@@ -15,7 +15,7 @@ const int RECEIVER_CHANNEL = LL_TIM_CHANNEL_CH1;
15 15
 TIM_TypeDef * DCMOTOR_TIMER = TIM2;
16 16
 const int DCMOTOR_CHANNEL = LL_TIM_CHANNEL_CH2;
17 17
 GPIO_TypeDef * DCMOTOR_GPIO = GPIOA;
18
-const int DCMOTOR_PIN_PWM = LL_GPIO_PIN_2;
18
+const int DCMOTOR_PIN_PWM = LL_GPIO_PIN_1;
19 19
 const int DCMOTOR_PIN_DIRECTION = LL_GPIO_PIN_2;
20 20
 
21 21
 

+ 8
- 3
Src/Roll.c View File

@@ -29,12 +29,17 @@ int Roll_getEmergencyState(void)
29 29
 	return Roll_isEmergencyState;
30 30
 }
31 31
 
32
+int current = 0;
33
+int angle= 0;
34
+
32 35
 void Roll_background(void)
33 36
 {
34
-	const int xAngle = Accelerometer_getAngle(ROLL_ADC, ROLL_X_CHANNEL);
37
+//	const int xAngle = Accelerometer_getAngle(ROLL_ADC, ROLL_X_CHANNEL);
35 38
 	const int yAngle = Accelerometer_getAngle(ROLL_ADC, ROLL_Y_CHANNEL);
36
-
37
-	const int currentState = abs(yAngle) >= 40;
39
+	angle = yAngle;
40
+	
41
+	const int currentState = Roll_isEmergencyState ? abs(yAngle) >= 10 : abs(yAngle) >= 40;
42
+	current = currentState;
38 43
 	if (Roll_isEmergencyState != currentState) {
39 44
 		Sail_setEmergency(currentState);
40 45
 		Roll_isEmergencyState = currentState;

+ 1
- 1
Src/Sail.c View File

@@ -11,7 +11,7 @@ TIM_TypeDef * ENCODER_TIMER = TIM3;
11 11
 GPIO_TypeDef * ENCODER_GPIO = GPIOA;
12 12
 const int ENCODER_PIN = LL_GPIO_PIN_5;
13 13
 
14
-const int RESET_ANGLE = 90;
14
+const int RESET_ANGLE = 0;
15 15
 
16 16
 int Sail_isEmergencyState = 0;
17 17
 

+ 1
- 1
Src/main.c View File

@@ -102,7 +102,7 @@ int main(void)
102 102
 	Scheduler_start();
103 103
 
104 104
   while (1) {
105
-		// Display_background(secCounter);
105
+		Display_background(secCounter);
106 106
   }
107 107
 
108 108
 

Loading…
Cancel
Save