Skip to content

Commit 42c9e68

Browse files
committed
add sine mode range and adjustable drag brake
1 parent b8ca903 commit 42c9e68

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

Mcu/g071/Inc/WS2812.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111
#include "main.h"
1212

13-
#endif /* INC_WS2812_H_ */
13+
1414

1515

1616
void send_LED_DMA();
1717
void WS2812_Init(void);
1818
void send_LED_RGB(uint8_t red, uint8_t green, uint8_t blue);
1919

20-
char dma_busy;
20+
extern char dma_busy;
21+
22+
23+
#endif /* INC_WS2812_H_ */

Mcu/g071/Src/WS2812.c

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "WS2812.h"
99

10+
char dma_busy;
1011
uint16_t led_Buffer[26] = {0,0,20,20,20,20,20,20,20,20,
1112
60,60,60,60,60,60,60,60,
1213
20,20,20,20,20,20,20,20};

Src/main.c

+63-17
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
*1.71 fix dshot for Ardupilot / Px4 FC
104104
*1.72 Fix telemetry output and add 1 second arming.
105105
*1.73 Fix false arming if no signal. Remove low rpm throttle protection below 300kv
106+
*1.74 Add Sine Mode range and drake brake strength adjustment
106107
*/
107108
#include <stdint.h>
108109
#include "main.h"
@@ -121,7 +122,7 @@
121122

122123

123124
#define VERSION_MAJOR 1
124-
#define VERSION_MINOR 73
125+
#define VERSION_MINOR 74
125126

126127
typedef struct __attribute__((packed)) {
127128
uint8_t version_major;
@@ -141,7 +142,7 @@ uint8_t EEPROM_VERSION;
141142
char BRUSHED_MODE = 0; // overrides everything else
142143
char RC_CAR_REVERSE = 0; // have to set bidirectional, comp_pwm off and stall protection off, no sinusoidal startup
143144
char GIMBAL_MODE = 0; // also sinusoidal_startup needs to be on.
144-
145+
char USE_HALL_SENSOR = 0;
145146
//move these to targets folder or peripherals for each mcu
146147

147148

@@ -157,6 +158,8 @@ uint16_t armed_timeout_count;
157158

158159
uint8_t desync_happened = 0;
159160
char maximum_throttle_change_ramp = 1;
161+
char sine_mode_changeover_thottle_level = 5;
162+
char drag_brake_strength = 10;
160163

161164
char crawler_mode = 0; // no longer used //
162165
uint16_t velocity_count = 0;
@@ -527,8 +530,24 @@ void loadEEpromSettings(){
527530
}else{
528531
RC_CAR_REVERSE = 0;
529532
}
533+
if(eepromBuffer[39] == 0x01){
534+
#ifdef HAS_HALL_SENSORS
535+
USE_HALL_SENSOR = 1;
536+
#else
537+
USE_HALL_SENSOR = 0;
538+
#endif
539+
}else{
540+
USE_HALL_SENSOR = 0;
541+
}
542+
if(eepromBuffer[40] > 4 && eepromBuffer[40] < 26){ // sine mode changeover 5-25 percent throttle
543+
sine_mode_changeover_thottle_level = eepromBuffer[40];
544+
}
545+
if(eepromBuffer[41] > 0 && eepromBuffer[41] < 11){ // drag brake 0-10
546+
drag_brake_strength = eepromBuffer[41];
547+
}
530548
}
531549

550+
532551
if(motor_kv < 300){
533552
low_rpm_throttle_limit = 0;
534553
}
@@ -790,6 +809,7 @@ if(!armed){
790809
if(!stepper_sine && !BRUSHED_MODE){
791810
if (input >= 47 +(80*use_sin_start) && armed){
792811
if (running == 0){
812+
allOff();
793813
if(!old_routine){
794814
startMotor();
795815
}
@@ -802,13 +822,14 @@ if(!armed){
802822
#endif
803823

804824
}
805-
// coasting = 0;
806-
// running = 1;
807825
if(use_sin_start){
808826
duty_cycle = map(input, 127, 2047, minimum_duty_cycle, TIMER1_MAX_ARR);
809827
}else{
810828
duty_cycle = map(input, 47, 2047, minimum_duty_cycle, TIMER1_MAX_ARR);
811829
}
830+
if(!RC_CAR_REVERSE){
831+
prop_brake_active = 0;
832+
}
812833
}
813834
if (input < 47 + (80*use_sin_start)){
814835
if(play_tone_flag != 0){
@@ -828,7 +849,6 @@ if(!armed){
828849
zero_crosses = 0;
829850
if(brake_on_stop){
830851
fullBrake();
831-
832852
}
833853
}
834854
if (RC_CAR_REVERSE && prop_brake_active) {
@@ -846,8 +866,11 @@ if(!armed){
846866
zero_crosses = 0;
847867
bad_count = 0;
848868
if(brake_on_stop){
849-
fullBrake();
850-
869+
if(!use_sin_start){
870+
duty_cycle = 1980 + drag_brake_strength*2;
871+
proportionalBrake();
872+
prop_brake_active = 1;
873+
}
851874
}else{
852875
allOff();
853876
duty_cycle = 0;
@@ -930,15 +953,11 @@ if (running){
930953
}
931954
adjusted_duty_cycle = ((duty_cycle * tim1_arr)/TIMER1_MAX_ARR)+1;
932955
}else{
933-
if(RC_CAR_REVERSE){
934956
if(prop_brake_active){
935957
adjusted_duty_cycle = TIMER1_MAX_ARR - ((duty_cycle * tim1_arr)/TIMER1_MAX_ARR)+1;
936958
}else{
937959
adjusted_duty_cycle = 0;
938960
}
939-
}else{
940-
adjusted_duty_cycle = 0;
941-
}
942961
}
943962
last_duty_cycle = duty_cycle;
944963

@@ -950,7 +969,7 @@ if (running){
950969
TIM1->CCR3 = adjusted_duty_cycle;
951970
}
952971
average_interval = e_com_time / 3;
953-
if(desync_check){
972+
if(desync_check && zero_crosses > 10){
954973
// if(average_interval < last_average_interval){
955974
//
956975
// }
@@ -1502,8 +1521,20 @@ if(newinput > 2000){
15021521
GPIOB->BSRR = LL_GPIO_PIN_3;
15031522
#endif
15041523
}else{
1505-
input=adjusted_input;
1506-
1524+
if(use_sin_start){
1525+
if(adjusted_input < 30){ // dead band ?
1526+
input= 0;
1527+
}
1528+
1529+
if(adjusted_input > 30 && adjusted_input < (sine_mode_changeover_thottle_level * 20)){
1530+
input= map(adjusted_input, 30 , (sine_mode_changeover_thottle_level * 20) , 47 ,160);
1531+
}
1532+
if(adjusted_input >= (sine_mode_changeover_thottle_level * 20)){
1533+
input = map(adjusted_input , (sine_mode_changeover_thottle_level * 20) ,2000 , 160, 2000);
1534+
}
1535+
}else{
1536+
input = adjusted_input;
1537+
}
15071538
}
15081539
if ( stepper_sine == 0){
15091540

@@ -1600,7 +1631,7 @@ if (old_routine && running){
16001631
if(input > 48 && armed){
16011632

16021633
if (input > 48 && input < 137){// sine wave stepper
1603-
// LL_TIM_DisableIT_UPDATE(TIM1);
1634+
16041635
maskPhaseInterrupts();
16051636
allpwm();
16061637
advanceincrement();
@@ -1621,9 +1652,11 @@ if(input > 48 && armed){
16211652
old_routine = 1;
16221653
commutation_interval = 9000;
16231654
average_interval = 9000;
1655+
last_average_interval = average_interval;
16241656
// minimum_duty_cycle = ;
16251657
INTERVAL_TIMER->CNT = 9000;
16261658
zero_crosses = 0;
1659+
prop_brake_active = 0;
16271660
step = changeover_step; // rising bemf on a same as position 0.
16281661
comStep(step);// rising bemf on a same as position 0.
16291662
LL_TIM_GenerateEvent_UPDATE(TIM1);
@@ -1632,10 +1665,23 @@ if(input > 48 && armed){
16321665
}
16331666

16341667
}else{
1635-
TIM1->CCR1 = 0; // set duty cycle to 50 out of 768 to start.
1668+
if(brake_on_stop){
1669+
duty_cycle = 1980 + drag_brake_strength*2;
1670+
adjusted_duty_cycle = TIMER1_MAX_ARR - ((duty_cycle * tim1_arr)/TIMER1_MAX_ARR)+1;
1671+
TIM1->ARR = tim1_arr;
1672+
TIM1->CCR1 = adjusted_duty_cycle;
1673+
TIM1->CCR2 = adjusted_duty_cycle;
1674+
TIM1->CCR3 = adjusted_duty_cycle;
1675+
proportionalBrake();
1676+
prop_brake_active = 1;
1677+
}else{
1678+
TIM1->CCR1 = 0;
16361679
TIM1->CCR2 = 0;
16371680
TIM1->CCR3 = 0;
1638-
fullBrake();
1681+
allOff();
1682+
}
1683+
1684+
16391685
}
16401686

16411687
}

f051makefile.mk

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ CFLAGS = $(MCU) $(VALUES) $(INCLUDES) -O2 -Wall -fdata-sections -ffunction-secti
3434
CFLAGS += -D$(TARGET)
3535
CFLAGS += -MMD -MP -MF $(@:%.bin=%.d)
3636

37-
#TARGETS := FD6288 MP6531 IFLIGHT TMOTOR55 TMOTOR45 HGLRC SISKIN DIATONE MAMBA_F40PRO MAMBA_F50PRO MAMBA_F60PRO WRAITH32 CRTEENSY_HILARIESC
38-
TARGETS := CRTEENSY_HILARIESC
37+
TARGETS := FD6288 MP6531 IFLIGHT TMOTOR55 TMOTOR45 HGLRC SISKIN DIATONE MAMBA_F40PRO MAMBA_F50PRO MAMBA_F60PRO WRAITH32 CRTEENSY_HILARIESC
3938

4039
# Working directories
4140
ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))

0 commit comments

Comments
 (0)