Tweaks afters connecting an actual fan

Changed PWM frequency to reduce fan motor hum
Added minimum value
Added custom value entry in the test application
This commit is contained in:
Mark van Renswoude 2017-09-02 10:51:21 +02:00
parent f7c199eb56
commit 0c142ed577
4 changed files with 71 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
AssettoCorsa/source/__history
AssettoCorsa/*.dproj.local
AssettoCorsa/*.identcache
AssettoCorsa/bin
AssettoCorsa/lib

View File

@ -4,7 +4,7 @@ object MainForm: TMainForm
BorderIcons = [biSystemMenu, biMinimize]
BorderStyle = bsSingle
Caption = 'SimulatorFans - Assetto Corsa'
ClientHeight = 309
ClientHeight = 390
ClientWidth = 645
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
@ -87,4 +87,29 @@ object MainForm: TMainForm
TabOrder = 7
OnClick = Button6Click
end
object Edit1: TEdit
Left = 288
Top = 253
Width = 75
Height = 21
TabOrder = 8
Text = '0'
end
object Button7: TButton
Left = 288
Top = 280
Width = 171
Height = 25
Caption = 'Set'
TabOrder = 9
OnClick = Button7Click
end
object Edit2: TEdit
Left = 384
Top = 253
Width = 75
Height = 21
TabOrder = 10
Text = '0'
end
end

View File

@ -22,6 +22,9 @@ type
Button4: TButton;
Button5: TButton;
Button6: TButton;
Edit1: TEdit;
Button7: TButton;
Edit2: TEdit;
procedure FormCreate(Sender: TObject);
procedure RefreshPortsButtonClick(Sender: TObject);
@ -31,6 +34,7 @@ type
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
FComPort: TComPort;
FReceived: string;
@ -70,6 +74,7 @@ end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
Edit1.Text := '255';
SendCommand('>SetFans:255,0',
procedure(Response: string)
begin
@ -80,6 +85,7 @@ end;
procedure TMainForm.Button3Click(Sender: TObject);
begin
Edit2.Text := '255';
SendCommand('>SetFans:0,255',
procedure(Response: string)
begin
@ -90,6 +96,8 @@ end;
procedure TMainForm.Button4Click(Sender: TObject);
begin
Edit1.Text := '0';
Edit2.Text := '0';
SendCommand('>SetFans:0,0',
procedure(Response: string)
begin
@ -100,6 +108,7 @@ end;
procedure TMainForm.Button5Click(Sender: TObject);
begin
Edit1.Text := '128';
SendCommand('>SetFans:128,0',
procedure(Response: string)
begin
@ -110,6 +119,7 @@ end;
procedure TMainForm.Button6Click(Sender: TObject);
begin
Edit2.Text := '128';
SendCommand('>SetFans:0,128',
procedure(Response: string)
begin
@ -118,9 +128,20 @@ begin
end);
end;
procedure TMainForm.Button7Click(Sender: TObject);
begin
SendCommand('>SetFans:' + Edit1.Text + ',' + Edit2.Text,
procedure(Response: string)
begin
if Response <> '<SetFans' then
ShowMessage('Invalid response: ' + Response);
end);
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
RefreshPorts;
PortComboBox.ItemIndex := Pred(PortComboBox.Items.Count);
end;

View File

@ -64,6 +64,10 @@ const byte FanPin[FanCount] =
#define StartingFansTime 200
// The minimum value at which the fan still spins reliably. Anything below
// will be considered as 0.
#define MinimumValue 32
/*
* Actual code. Lasciate ogne speranza, voi ch'intrate.
*/
@ -94,6 +98,17 @@ void setup()
for (byte fan = 0; fan < FanCount; fan++)
pinMode(FanPin[fan], OUTPUT);
// Set all clock select bits to clk/1024. This will result in a 30 ~ 60 Hz PWM frequency.
// The default frequency causes annoying noises in the fan, the low frequency is still
// audible but good enough since the game sounds should easily drown that out.
// It messes up the millis() function though, so compensate for that later on.
TCCR0B = TCCR0B & 0b11111000 | 0b101;
TCCR1B = TCCR1B & 0b11111000 | 0b101;
TCCR2B = TCCR2B & 0b11111000 | 0b101;
// 64 is the default prescaler
#define correctedMillis() (millis() * (1024 / 64))
// Set up serial communication (through USB or the default pins)
// 19.2k is fast enough for our purpose, and according to the ATMega's datasheet
// has a low error percentage across the common oscillator frequencies.
@ -108,7 +123,7 @@ char* token;
void loop()
{
currentTime = millis();
currentTime = correctedMillis();
checkStartingFans();
if (Serial.available() > 0)
@ -202,7 +217,11 @@ void checkStartingFans()
void setFan(byte fan, byte value)
{
if ((fanStatus[fan].value == 0 || fanStatus[fan].startTime > 0) && value > 0)
byte correctedValue = value;
if (correctedValue < MinimumValue)
correctedValue = 0;
if ((fanStatus[fan].value == 0 || fanStatus[fan].startTime > 0) && correctedValue > 0)
{
// Fan was off or still starting up, start with full power to kick it off
analogWrite(FanPin[fan], 255);
@ -216,9 +235,8 @@ void setFan(byte fan, byte value)
{
// Already running, simply change the speed and reset the start time if necessary
fanStatus[fan].startTime = 0;
analogWrite(FanPin[fan], value);
analogWrite(FanPin[fan], correctedValue);
}
fanStatus[fan].value = value;
fanStatus[fan].value = correctedValue;
}