rusefillc 2022-07-14 15:45:33 -04:00
parent 975329fcc7
commit f6afb2a9fa
2 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,18 @@
txPayload = {}
function onTick()
auxV = getAuxAnalog(0)
print('Hello analog ' .. auxV )
-- first byte: integer part, would be autoboxed to int
txPayload[1] = auxV
-- second byte: fractional part, would be autoboxed to int, overflow would be ignored
txPayload[2] = auxV * 256;
auxV = getAuxAnalog(1)
print('Hello analog ' .. auxV )
txPayload[3] = auxV
txPayload[4] = auxV * 256;
auxV = getAuxAnalog(2)
print('Hello analog ' .. auxV )
txPayload[5] = auxV
txPayload[6] = auxV * 256;
txCan(1, 0x600, 1, txPayload)
end

View File

@ -0,0 +1,50 @@
startPwm(0, 800, 0.1)
-- direction
startPwm(1, 80, 1.0)
-- disable
startPwm(2, 80, 0.0)
pid = Pid.new(2, 0, 0, -100, 100)
biasCurveIndex = findCurveIndex("bias")
canRxAdd(0x600)
voltageFromCan = 0
function onCanRx(bus, id, dlc, data)
print('got CAN id=' .. id .. ' dlc=' .. dlc)
voltageFromCan = data[2] / 256.0 + data[1]
end
function onTick()
local targetVoltage = getAuxAnalog(0)
-- local target = interpolate(1, 0, 3.5, 100, targetVoltage)
local target = interpolate(1, 0, 3.5, 100, voltageFromCan)
-- clamp 0 to 100
target = math.max(0, target)
target = math.min(100, target)
print('Decoded target: ' .. target)
local tps = getSensor("TPS1")
tps = (tps == nil and 'invalid TPS' or tps)
print('Tps ' .. tps)
local output = pid:get(target, tps)
local bias = curve(biasCurveIndex, target)
print('bias ' .. bias)
local duty = (bias + output) / 100
isPositive = duty > 0;
pwmValue = isPositive and duty or -duty
setPwmDuty(0, pwmValue)
dirValue = isPositive and 1 or 0;
setPwmDuty(1, dirValue)
print('pwm ' .. pwmValue .. ' dir ' .. dirValue)
print('')
end