Skip to content

Instantly share code, notes, and snippets.

@robertcedwards
Created August 18, 2015 23:58
Show Gist options
  • Save robertcedwards/2f7a061af8ccc987aab2 to your computer and use it in GitHub Desktop.
Save robertcedwards/2f7a061af8ccc987aab2 to your computer and use it in GitHub Desktop.
Assetto Corsa Fan Control using Python API & Arduino
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "dll"))
import ac
import acsys
import serial
import time
#ser = serial.Serial(3, 9600)
ser = serial.Serial(port = 'COM4', baudrate = 9600, timeout = 0)
count = 0
def acMain(acVersion):
# do something with serial library
global ser
ac.log("called acMain()")
time.sleep(3)
#ser.write("1:0;".encode())
ser.write("3:17000;".encode())
return "Arduino Serial"
def acUpdate(deltaT):
global ser,ac,acsys,count
if count == 15:
value=ac.getCarState(0,acsys.CS.SpeedMPH)
value = str(round(value))
toSend="1:" + value + ";"
ser.write(toSend.encode())
gear=ac.getCarState(0,acsys.CS.Gear)
gear = int(gear)
gear = gear - 1
if gear == 0:
gear = "n"
elif gear == -1:
gear = "r"
else:
gear = str(gear)
toSendGear = "2:" + gear + ";"
ser.write(toSendGear.encode())
count = 0
else:
count = count + 1
def acShutdown():
global ser
ac.log("called acShutdown()")
ser.close()
#include <AccelStepper.h>
AccelStepper stepper(1, 2, 3);
const String keyRev = "1";
int speed;
int previous;
String vRev = "0";
int delimiter = 59; // means: ;
char data[80];
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
stepper.setMaxSpeed(1000);
stepper.setSpeed(100);
stepper.setAcceleration(500);
}
void loop()
{
while (Serial.available() > 0) {
Serial.readBytesUntil(delimiter, data, 80);
String command = String(data);
if (command.substring(0,1) == keyRev) {
vRev = command.substring(2);
}
memset(data, '\0', 80);
}
if (vRev.toInt() > 5) {
speed = vRev.toInt() * 15;
stepper.moveTo(speed);
stepper.run();
//previous = speed;
}
else {
digitalWrite(11, LOW);
}
}
@robertcedwards
Copy link
Author

@demirkolerme & @andersonvnicius & @pippalTA It looks like you might need to enable Python in Assetto Corsa in
"Documents\Assetto Corsa\cfg\gameplay.ini"
Look for this line and change it to 1
[PYTHON]
ENABLE_PYTHON=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment