I'm trying to get a wemos motor hat to work with Lua. (wemos D1 mini)
But gets this error when running the "test.lua" file and can not get what it is.
something is wrong with the line "motor1.speed(2000)"
**************************************************************************
test.lua:7: attempt to index global 'motor1' (a nil value)
stack traceback:
test.lua:7: in main chunk
[C]: in function 'dofile'
stdin:1: in main chunk
*****************************************************************************
test,lua:
dofile("motor-test.lua")
motor1 = Motor(0x00)
motor2 = Motor(0x01)
motor1.speed(2000)
--motor2.speed(-1000)
File "motortest.lua" (run by "test.lua")
local busid = 0
local scl = 2
local sda = 1
local dev_addr = 0x30
_STATE_BRAKE = 0x00
_STATE_RIGHT =0x01
_STATE_LEFT = 0x02
_STATE_STOP = 0x03
_STATE_SLEEP = 0x04
i2c.setup(busid, sda, scl, i2c.SLOW)
local Motor = {}
Motor.__index = Motor
print("1")
setmetatable(Motor, {
__call = function (cls, ...)
return cls.new(...)
end,
print("2")
})
function Motor.new(index)
local self = setmetatable({}, Motor)
self._index = index
self._speed = 0
self._state = 0
self.frequency = 1000 --(1000 > 10000)
print("Motor_new end" )
end
function Motor:__tostring()
return "Motor (" .. self._index .. ", " .. self._speed .. ", " .. self._state .. ")"
end
print("3")
function Motor:frequency(frequency)
print("X")
i2c.start(busid)
i2c.address(busid, dev_addr, i2c.TRANSMITTER)
print(self._index)
i2c.write(busid, bit.bor(0x00, self._index), struct.pack('>BH', 0x00, frequency))
i2c.stop(busid)
print("motor-freq")
end
function Motor:update()
i2c.start(busid)
i2c.address(busid, dev_addr, i2c.TRANSMITTER)
i2c.write(busid, bit.bor(0x10, self._index), struct.pack('>BH', self._state, self._speed))
i2c.stop(busid)
print("motor-update")
end
function Motor:speed(value)
print("value=")
if value>0 and value<=-10000 then
self._speed = value
self._state = _STATE_RIGHT
elseif value<0 and value>=-10000 then
self._speed = -1 * value
self._state = _STATE_LEFT
elseif value == 0 then
self._speed = 0
self._state = _STATE_STOP
else
print("Wrong Value")
end
print("Motor:speed")
self.update()
end
function Motor:sleep()
self._speed = 0
self._state = _STATE_SLEEP
self.update()
print("Motor:sleep")
end
function Motor:brake()
self._speed = 0
self._state = _STATE_BRAKE
self.update()
end