--[[ This function is executed every time you press the 'execute' button ]] function init() -- blue beacon mean it's the right information robot.colored_blob_omnidirectional_camera.enable() timePeriod = 0 timer = 0 turnaround = 0 actualColor = 1 mu = 0.01 randMax = 100 setBeacon(actualColor) end --moving ______________________________________________________________________________________________________________________________________________ function driveAsCar(forwardSpeed, angularSpeed) leftSpeed = forwardSpeed - angularSpeed rightSpeed = forwardSpeed + angularSpeed robot.wheels.set_velocity(leftSpeed,rightSpeed) end -- _____________________________________________________________________________________________________ --RandomExponential______________________________________________________________________________________ function randomExp(mu) u = math.random(0, randMax) / (randMax+1) return (-(math.log(1-u)) / mu) end -- ____________________________________________________________________________________________________ --obstacle avoidance __________________________________________________________________________________ function avoiding(proxiLeft, proxiRight, foward) for i = 1,5 do proxiLeft = proxiLeft + robot.proximity[i].value end for i = 19,24 do proxiRight = proxiRight + robot.proximity[i].value end if (proxiLeft ~= 0) and (proxiRight ~= 0) then --log("turn around") driveAsCar(0,40) elseif (proxiLeft ~= 0) and (proxiRight == 0) then --log("turn left") driveAsCar(30,-35) elseif (proxiLeft == 0) and (proxiRight ~= 0) then --log("turn right") driveAsCar(40,35) elseif (foward == 1) then driveAsCar(40,0) elseif (foward == 0) then if (turnaround == 0) then y = math.random(-40, 40) end if (turnaround < 10) then driveAsCar(20, y) turnaround = turnaround + 1 elseif (turnaround == 10) then driveAsCar(20, y) turnaround = 0 end end end -- change the color of the beacon -- ---------------------------------------------------------- function setBeacon(actualColor) if actualColor == 1 then robot.leds.set_single_color(13, "blue") elseif actualColor == -1 then robot.leds.set_single_color(13, "red") end end -- Get the information around this -- ------------------------------------------------------------------ function getInformation() if (#robot.colored_blob_omnidirectional_camera > 0) then selector = math.random(1,#robot.colored_blob_omnidirectional_camera) -- log("selector blue" .. selector) blop = robot.colored_blob_omnidirectional_camera[selector] if blop.color.red == 255 then actualColor = -1 setBeacon(actualColor) -- log ("changeit") elseif blop.color.blue == 255 then actualColor = 1 setBeacon(actualColor) -- log ("changeit") end end end --[[ This function is executed at each time step _______________________________________________________________________ It must contain the logic of your controller ]] function step() if timePeriod == 0 then exp = randomExp(mu) exp = math.floor(exp) timePeriod = timePeriod +1 elseif (timePeriod == exp) then getInformation() timePeriod = 0 else timePeriod = timePeriod +1 end x = math.random() if (x > 0.5) and (timer == 0) then foward = 1 elseif (x <= 0.5) and (timer == 0) then foward = 0 end if (timer == 50) then timer = 0 avoiding(0, 0, foward) else timer = timer + 1 avoiding(0, 0, foward) end end -- ______________________________________________________________________________________________________________________________________________ --[[ This function is executed every time you press the 'reset' button in the GUI. It is supposed to restore the state of the controller to whatever it was right after init() was called. The state of sensors and actuators is reset automatically by ARGoS. ]] function reset() -- put your code here end --[[ This function is executed only once, when the robot is removed from the simulation ]] function destroy() name = (robot.id .. ".txt") -- Opens a file in write mode file = io.open(name, "w") -- appends a word test to the last line of the file -- sets the default output file as test.lua io.output(file) io.write(actualColor) -- closes the open file io.close(file) end