Open FFBoard
Open source force feedback firmware
voltagesense.cpp
Go to the documentation of this file.
1/*
2 * voltagesense.cpp
3 *
4 * Created on: Feb 1, 2020
5 * Author: Yannick
6 */
7
8#include "global_callbacks.h"
9#include "constants.h"
10#include "voltagesense.h"
11#include "ErrorHandler.h"
12#include "AdcHandler.h"
13#include "FastAvg.h"
14
15bool braking_flag = false;
16int32_t maxVoltage = 65000; // Force braking
17int32_t voltageDiffActivate = 5000;
18int32_t voltageDiffDeactivate = 4000;
19float vSenseMult = VOLTAGE_MULT_DEFAULT;
20bool brake_failure = false;
21int32_t minVoltage = 6500;
22
23uint32_t brakeActiveTime = 0;
25
26/*
27 * Multiplier to convert ADC counts to a sensed voltage on the vInt and vExt pins.
28 */
29void setVSenseMult(float vSenseMultiplier){
30 vSenseMult = vSenseMultiplier;
31}
32
33/*
34 * vMax: maximum voltage where the brake pin goes always high
35 * vdiffAct: difference between vInt and vExt to activate brake pin
36 * vdiffDeact: difference when to deactivate brake pin again (must be lower than vdiffAct)
37 * Set vMax = 0 to completely deactivate the brake resistor function. DANGEROUS
38 *
39 */
40void setupBrakePin(int32_t vdiffAct,int32_t vdiffDeact,int32_t vMax){
41 maxVoltage = vMax;
42 voltageDiffActivate = vdiffAct;
43 voltageDiffDeactivate = vdiffDeact;
44}
45
46float adcValToVoltage(uint32_t adcval){
47 if(ADC_INTREF_VOL == 0){
48 return 0; // Divbyzero
49 }
50 return __LL_ADC_CALC_DATA_TO_VOLTAGE(ADC_INTREF_VOL,adcval,VSENSE_ADC_RES);
51}
52
53int32_t getIntV(){
54 return adcValToVoltage(VSENSE_ADC_BUF[ADC_CHAN_VINT]) * vSenseMult;
55
56}
57
58int32_t getExtV(){
59 return adcValToVoltage(VSENSE_ADC_BUF[ADC_CHAN_VEXT]) * vSenseMult;
60}
61
63 if(maxVoltage == 0 || brake_failure){
64 return;
65 }
66 int32_t vint = getIntV();
67 int32_t vext = getExtV();
68
69 if(vint < minVoltage && vext < minVoltage){
70 return; // Do not enable if device is unpowered (just measuring usb leakage)
71 }
72
73 bool lastBrakingFlag = braking_flag;
75 //(ADC_BUF[ADC_CHAN_VINT] > ADC_BUF[ADC_CHAN_VEXT]+400 || (ADC_BUF[ADC_CHAN_VINT] > 3000));
76 if(braking_flag != lastBrakingFlag){
77 if(braking_flag){
78 brakeActiveTime = HAL_GetTick();
79 }else{
81 }
82 }
83 if(brakeActiveTime && HAL_GetTick() - brakeActiveTime > 5000){
84 // Brake resistor active over 5s. Shut down.
85 brake_failure = true;
86 HAL_GPIO_WritePin(DRV_BRAKE_GPIO_Port,DRV_BRAKE_Pin, GPIO_PIN_RESET);
87 // can not create error here in ISR
88 //ErrorHandler::addError(resError);
89 }else{
90 HAL_GPIO_WritePin(DRV_BRAKE_GPIO_Port,DRV_BRAKE_Pin, braking_flag ? GPIO_PIN_SET:GPIO_PIN_RESET);
91 }
92
93}
94
96__weak int32_t getChipTemp(){
97#if !defined(TEMPSENSOR_ADC_VAL) || !defined(__LL_ADC_CALC_TEMPERATURE)
98 return 0;
99#else
100 if(!TEMPSENSOR_ADC_VAL || !ADC_INTREF_VOL){
101 return 0; // divby0 risk
102 }
103 return chipTempAvg.addValue(__LL_ADC_CALC_TEMPERATURE(ADC_INTREF_VOL,TEMPSENSOR_ADC_VAL,TEMPSENSOR_ADC_RES));
104#endif
105
106}
@ brakeResistorFailure
void brakeCheck()
const Error resError
int32_t getIntV()
FastMovingAverage< int32_t > chipTempAvg
float adcValToVoltage(uint32_t adcval)
__weak int32_t getChipTemp()
int32_t getExtV()
bool braking_flag
void setVSenseMult(float vSenseMultiplier)
bool brake_failure
void setupBrakePin(int32_t vdiffAct, int32_t vdiffDeact, int32_t vMax)
float vSenseMult
int32_t maxVoltage
uint32_t brakeActiveTime
int32_t minVoltage
int32_t voltageDiffDeactivate
int32_t voltageDiffActivate