nameless12242312

Electric motor class

Oct 7th, 2024 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.07 KB | Gaming | 0 0
  1. --Define the motor class
  2. Motor = {}
  3. Motor.__index = Motor
  4.  
  5. --contructor for a new electric motor
  6. function Motor:new(id,max,min)
  7.     --input validation for electric_motor
  8.     assert(type(id) == "string", "the id: "..id.. "should be a string")
  9.     assert(type(max) == "number", "the maximum speed must be a number")
  10.     assert(type(min) == "number", "the minimum speed must be a number")
  11.  
  12.     --finding the physical electric motor
  13.     local peripheral_name = "electric_motor_" .. tostring(id)
  14.     local motor = peripheral.wrap(peripheral_name)
  15.  
  16.     --validate that the motor was found
  17.     if not motor then
  18.         error("the motor of name: "..peripheral_name.." could not be found")
  19.     end
  20.  
  21.     if type(motor.setSpeed) ~= "function" then
  22.         error("the found peripheral was not an electric motor")
  23.     end
  24.  
  25.     --create a new pid controller specific to the electric motor instance
  26.     local PIDController = require("PIDController")
  27.     local Controller = PIDController:newController(0.1,0.1,0.1,max,min)
  28.  
  29.     --create the metatable for the electric motor's properties
  30.     local self_obj = setmetatable({}, Motor)
  31.     self_obj.motor = motor
  32.     self_obj.id = peripheral_name
  33.     self_obj.min_speed = min
  34.     self_obj.max_speed = max
  35.     self_obj.controller = Controller
  36.     self_obj.status = nil
  37.  
  38.     --return the motor's instance
  39.     return self_obj
  40. end
  41.  
  42. --sets another motor as a slave to this motor
  43. function Motor:setSlave(...)  
  44.     local args = {...}
  45.     --verify input perameters
  46.     assert(type(args[1]) == "table","the first input must be a motor object")
  47.     assert(type(args[2]) == "boolean" or "nil","the second input must be a boolean declaring whether the follow is inverted")
  48.  
  49.     --Initialization of slaves list
  50.     self.slaves = self.slaves or {}
  51.  
  52.     --set status of the slave motor depending on the inverted parameter
  53.     if args[2] == true then
  54.         args[1].status = "Inv_slave"
  55.         self.status = "master"
  56.  
  57.         --negating motor parameters for inversion
  58.         args[1].max_speed = -self.min_speed
  59.         args[1].min_speed = -self.max_speed
  60.     else
  61.         args[1].status = "slave"
  62.         self.status = "master"
  63.     end
  64.  
  65.     --append the motor to the list of the master's slaves
  66.     table.insert(self.slaves, args[1])
  67. end --unimplemented
  68.  
  69. --sets another motor as a slave to this motor
  70. function Motor:getSlaves()
  71.     slaves = {}
  72.     for i in #self.slaves do
  73.         table.insert(slaves,i)
  74.     end
  75.  
  76.     return slaves
  77. end --unimplemented
  78.  
  79. -- Helper function for setting the motor and all slaves' speeds
  80. function Motor:run(set_point)
  81.     self.motor:setSpeed(set_point)
  82. end
  83.  
  84. -- helper function for retrieving the motors speed
  85. function Motor:getSpeed()
  86.     return self.motor.getSpeed()
  87. end
  88.  
  89. -- return the status of the current motor
  90. function Motor:getStatus()
  91.     return self.motor.status
  92. end
  93.  
  94. -- return the id of the current motor
  95. function Motor:getId()
  96.     return self.motor.id
  97. end
  98.  
  99. --helper function for stopping the motor
  100. function Motor:stop()
  101.     Motor:run(0)
  102. end
  103.  
  104. --return the motor class
  105. return Motor
Advertisement
Add Comment
Please, Sign In to add comment