Skip to content

Commit

Permalink
Ts80 Tuned a bit better, Ts100 WiP
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralim committed Jul 13, 2019
1 parent 74b225c commit c5409f4
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 142 deletions.
2 changes: 1 addition & 1 deletion workspace/TS100/inc/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern "C" {
enum Orientation {
ORIENTATION_LEFT_HAND = 0, ORIENTATION_RIGHT_HAND = 1, ORIENTATION_FLAT = 3
};

#define PID_TIM_HZ (16)
#if defined(MODEL_TS100) + defined(MODEL_TS80) > 1
#error "Multiple models defined!"
#elif defined(MODEL_TS100) + defined(MODEL_TS80) == 0
Expand Down
15 changes: 7 additions & 8 deletions workspace/TS100/inc/power.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
* Power.hpp
*
* Created on: 28 Oct, 2018
* Authors: Ben V. Brown, David Hilton
* Authors: Ben V. Brown, David Hilton (David's Idea)
*/

#include "stdint.h"
#include <history.hpp>

#include "hardware.h"
#ifndef POWER_HPP_
#define POWER_HPP_

const uint8_t hz = 32;//PID loop rate
const uint8_t oscillationPeriod = 3.5 * hz; // dampening look back tuning
const uint8_t oscillationPeriod = 4 * PID_TIM_HZ; // I term look back value
extern history<uint32_t, oscillationPeriod> milliWattHistory;
void setupPower(uint8_t resistance);

int32_t tempToMilliWatts(int32_t rawTemp, uint16_t mass, uint8_t rawC);
int32_t tempToMilliWatts(int32_t rawTemp, uint8_t rawC);
void setTipMilliWatts(int32_t mw);
uint8_t milliWattsToPWM(int32_t milliWatts, uint8_t divisor,uint8_t sample=0);
int32_t PWMToMilliWatts(uint8_t pwm, uint8_t divisor);
uint8_t milliWattsToPWM(int32_t milliWatts, uint8_t divisor,
uint8_t sample = 0);
int32_t PWMToMilliWatts(uint8_t pwm, uint8_t divisor, uint8_t sample = 0);

#endif /* POWER_HPP_ */
26 changes: 13 additions & 13 deletions workspace/TS100/src/Setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ static void MX_TIM3_Init(void) {
htim3.Instance = TIM3;
htim3.Init.Prescaler = 8;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 100; // 10 Khz PWM freq
htim3.Init.Period = 400; // 5 Khz PWM freq
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4; // 4mhz before div
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;//Preload the ARR register (though we dont use this)
HAL_TIM_Base_Init(&htim3);

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
Expand All @@ -279,7 +279,7 @@ static void MX_TIM3_Init(void) {
HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 50;
sConfigOC.Pulse = 80;//80% duty cycle, that is AC coupled through the cap
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, PWM_Out_CHANNEL);
Expand All @@ -291,10 +291,10 @@ static void MX_TIM3_Init(void) {
*/
GPIO_InitStruct.Pin = PWM_Out_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;//We would like sharp rising edges
HAL_GPIO_Init(PWM_Out_GPIO_Port, &GPIO_InitStruct);
#ifdef MODEL_TS100
// Remap TIM3_CH1 to be on pB4
// Remap TIM3_CH1 to be on PB4
__HAL_AFIO_REMAP_TIM3_PARTIAL();
#else
// No re-map required
Expand All @@ -314,14 +314,17 @@ static void MX_TIM2_Init(void) {
// Timer 2 is fairly slow as its being used to run the PWM and trigger the ADC
// in the PWM off time.
htim2.Instance = TIM2;
htim2.Init.Prescaler = 785; // pwm out is 10k from tim3, we want to run our PWM at around 10hz or slower on the output stage
htim2.Init.Prescaler = 2000; //1mhz tick rate/800 = 1.25 KHz tick rate

// pwm out is 10k from tim3, we want to run our PWM at around 10hz or slower on the output stage
// The input is 1mhz after the div/4, so divide this by 785 to give around 4Hz output change rate
//Trade off is the slower the PWM output the slower we can respond and we gain temperature accuracy in settling time,
//But it increases the time delay between the heat cycle and the measurement and calculate cycle
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 255 + 60;
htim2.Init.Period = 255 + 20;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4; // 4mhz before divide
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
htim2.Init.RepetitionCounter=0;
HAL_TIM_Base_Init(&htim2);

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
Expand All @@ -335,7 +338,8 @@ static void MX_TIM2_Init(void) {
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 255 + 50; //255 is the largest time period of the drive signal, and the 50 offsets this around 5ms afterwards
sConfigOC.Pulse = 255 + 10;
//255 is the largest time period of the drive signal, and then offset ADC sample to be a bit delayed after this
/*
* It takes 4 milliseconds for output to be stable after PWM turns off.
* Assume ADC samples in 0.5ms
Expand All @@ -344,11 +348,7 @@ static void MX_TIM2_Init(void) {
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
sConfigOC.Pulse = 0;//default to entirely off
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4);

HAL_TIM_Base_Start_IT(&htim2);
Expand Down
49 changes: 33 additions & 16 deletions workspace/TS100/src/hardware.c → workspace/TS100/src/hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "FreeRTOS.h"
#include "stm32f1xx_hal.h"
#include "cmsis_os.h"
#include "history.hpp"
volatile uint16_t PWMSafetyTimer = 0;
volatile int16_t CalibrationTempOffset = 0;
uint16_t tipGainCalValue = 0;
Expand Down Expand Up @@ -68,15 +69,31 @@ uint16_t ftoTipMeasurement(uint16_t temp) {
}

uint16_t getTipInstantTemperature() {
uint16_t sum;
sum = hadc1.Instance->JDR1;
sum += hadc1.Instance->JDR2;
sum += hadc1.Instance->JDR3;
sum += hadc1.Instance->JDR4;
sum += hadc2.Instance->JDR1;
sum += hadc2.Instance->JDR2;
sum += hadc2.Instance->JDR3;
sum += hadc2.Instance->JDR4;
uint16_t sum = 0; // 12 bit readings * 8 -> 15 bits
uint16_t readings[8];
//Looking to reject the highest outlier readings.
//As on some hardware these samples can run into the op-amp recovery time
//Once this time is up the signal stabilises quickly, so no need to reject minimums
readings[0] = hadc1.Instance->JDR1;
readings[1] = hadc1.Instance->JDR2;
readings[2] = hadc1.Instance->JDR3;
readings[3] = hadc1.Instance->JDR4;
readings[4] = hadc2.Instance->JDR1;
readings[5] = hadc2.Instance->JDR2;
readings[6] = hadc2.Instance->JDR3;
readings[7] = hadc2.Instance->JDR4;
uint8_t minID = 0, maxID = 0;
for (int i = 0; i < 8; i++) {
if (readings[i] < readings[minID])
minID = i;
else if (readings[i] > readings[maxID])
maxID = i;
}
for (int i = 0; i < 8; i++) {
if (i != maxID)
sum += readings[i];
}
sum += readings[minID]; //Duplicate the min to make up for the missing max value
return sum; // 8x over sample
}
/*
Expand Down Expand Up @@ -117,15 +134,17 @@ uint16_t lookupTipDefaultCalValue(enum TipType tipID) {
}
#endif
}
//2 second filter (ADC is PID_TIM_HZ Hz)
history<uint16_t, PID_TIM_HZ*4> rawTempFilter = { { 0 }, 0, 0 };

uint16_t getTipRawTemp(uint8_t refresh) {
static uint16_t lastSample = 0;

if (refresh) {
lastSample = getTipInstantTemperature();
uint16_t lastSample = getTipInstantTemperature();
rawTempFilter.update(lastSample);
return lastSample;
} else {
return rawTempFilter.average();
}

return lastSample;
}

uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) {
Expand Down Expand Up @@ -237,8 +256,6 @@ void startQC(uint16_t divisor) {
// Pre check that the input could be >5V already, and if so, dont both
// negotiating as someone is feeding in hv
uint16_t vin = getInputVoltageX10(divisor, 1);
if (vin > 150)
return; // Over voltage
if (vin > 100) {
QCMode = 1; // ALready at ~12V
return;
Expand Down
Loading

0 comments on commit c5409f4

Please sign in to comment.