Difference between revisions of "Lua interface"

From Studio Kousagi Wiki
Jump to: navigation, search
(Example program)
(Example program)
 
Line 27: Line 27:
 
   netv.sleep(0.05)
 
   netv.sleep(0.05)
 
  end
 
  end
 
 
   
 
   
 
  -- Now, spin the motor backwards for a few seconds
 
  -- Now, spin the motor backwards for a few seconds

Latest revision as of 09:42, 16 December 2011

The primary language used to develop for NeTV is Lua. This language was chosen because it's both easy to work with and is very fast.

Motor control library API

There is a motor control API available. To import the library, run:

require("motor")

Example program

require("motor")

--[=[ Set motor #3 to full speed forward for
   the duration of the loops, so that it has something
   interesting to do. ]=]
motor.set_speed(3, 255)

-- Set the servo, which sits on channel 2, to be a servo
motor.set_type(2, 's')

-- Rotate the servo clockwise, from 0° to 180°
for v = 0, 180, 1 do
  motor.set_angle(2, v)
  netv.sleep(0.05)
end

-- Now rotate it counterclockwise, from 180° to 0°
for v = 180, 0, -1 do
  motor.set_angle(2, v)
  netv.sleep(0.05)
end

-- Now, spin the motor backwards for a few seconds
motor.set_speed(3, -255)
netv.sleep(2)

-- Reset everything to 0
motor.set_angle(2, 180)
motor.set_speed(3, 0)

Digital I/O

motor.set_digital(v) - Sets the digital output pins to the value v. All pins are set at the same time.

Motor control

motor.set_type(m, t) - Sets the type of motor m to the type t. To switch to a servo, specify "s" for t. To switch to a motor, specify "m" for t. Note that you can only set channels 1 and 2 to servo.

motor.set_speed(m, v) - Sets the current speed of motor m to the speed v. The speed is specified from -255 to 255.

motor.set_angle(m, a) - Sets the angle of motor m to a degrees. The angle must be between 0 and 180.

Analog input

motor.get_adc(a) - Gets the current value from ADC a. There are 8 ADCs numbered 0-7. The return value is from 0 - 255.

Miscellaneous functions

netv.sleep(t) - Sleep for t seconds. t' is a Lua Number, and so may be non-integer, e.g. 0.5.