Skip to main content

Tutorial: Building a Robot

This tutorial covers the fundamentals of robotics with HIL LISP, from motor control to autonomous navigation (Siciliano & Khatib, 2016).

Robot Hardware​

A basic HIL robot requires:

ComponentConnectionPurpose
L298N Motor DriverDigital pins 5,6,7,8DC motor control
Left MotorL298N OUT1/OUT2Left wheel
Right MotorL298N OUT3/OUT4Right wheel
HC-SR04 UltrasonicD9 (trig), D10 (echo)Obstacle detection
Servo MotorD9Sensor sweep
Battery PackMotor driver VINPower supply

Motor Control​

Basic Motor Driver (L298N)​

; Motor pins
(SET left-in1 5)
(SET left-in2 6)
(SET right-in3 7)
(SET right-in4 8)

; Initialize motor pins
(PIN-MODE D left-in1 1)
(PIN-MODE D left-in2 1)
(PIN-MODE D right-in3 1)
(PIN-MODE D right-in4 1)

; Motor control functions
(DEFUN MOTOR-LEFT (dir)
(DO
(WRITE D left-in1 dir)
(WRITE D left-in2 (- 1 dir))))

(DEFUN MOTOR-RIGHT (dir)
(DO
(WRITE D right-in3 dir)
(WRITE D right-in4 (- 1 dir))))

Movement Functions​

; Forward
(DEFUN FORWARD ()
(DO
(MOTOR-LEFT 1)
(MOTOR-RIGHT 1)
(LOG "Moving forward")))

; Backward
(DEFUN BACKWARD ()
(DO
(MOTOR-LEFT 0)
(MOTOR-RIGHT 0)
(LOG "Moving backward")))

; Turn left
(DEFUN TURN-LEFT ()
(DO
(MOTOR-LEFT 0)
(MOTOR-RIGHT 1)
(LOG "Turning left")))

; Turn right
(DEFUN TURN-RIGHT ()
(DO
(MOTOR-LEFT 1)
(MOTOR-RIGHT 0)
(LOG "Turning right")))

; Stop
(DEFUN STOP ()
(DO
(MOTOR-LEFT 0)
(MOTOR-RIGHT 0)
(WRITE D left-in1 0)
(WRITE D left-in2 0)
(WRITE D right-in3 0)
(WRITE D right-in4 0)
(LOG "Stopped")))

Obstacle Avoidance​

Distance Measurement​

(DEFUN DISTANCE ()
(DO
(WRITE D 9 1)
(DLY-US 10)
(WRITE D 9 0)
(SET dur (PULSE D 10 30000))
(/ (* dur 343) 20000)))

Simple Avoidance Algorithm​

; Obstacle avoidance loop
(SET safe-distance 30)

(WHILE 1
(DO
(SET dist (DISTANCE))
(LOG "Distance: " dist "cm")

(IF (> dist safe-distance)
(FORWARD)
(DO
(STOP)
(BACKWARD)
(DELAY 300)
(IF (== (RANDOM 0 1) 0)
(TURN-LEFT)
(TURN-RIGHT))
(DELAY 300)))
(DELAY 100)))

Advanced: Servo Sweep​

Scan for obstacles in multiple directions:

(DEFUN SCAN-DIRECTION (angle)
(DO
(SERVO 9 angle)
(DELAY 500)
(DISTANCE)))

(DEFUN FIND-CLEAR-PATH ()
(DO
(SET left-dist (SCAN-DIRECTION 45))
(SET right-dist (SCAN-DIRECTION 135))
(IF (> left-dist right-dist) 45 135)))

; Enhanced avoidance
(WHILE 1
(DO
(SET dist (DISTANCE))
(IF (< dist safe-distance)
(DO
(STOP)
(SET dir (FIND-CLEAR-PATH))
(SERVO 9 90)
(IF (== dir 45) (TURN-LEFT) (TURN-RIGHT))
(DELAY 500)
(SERVO 9 90)))
(FORWARD)
(DELAY 100)))

Line Following​

IR Line Sensor Array​

; IR sensors on A0 (left), A1 (center), A2 (right)
(SET threshold 500)

(DEFUN READ-LINE ()
(DO
(SET left (READ A 0))
(SET center (READ A 1))
(SET right (READ A 2))
(COND
((AND (< left threshold) (> center threshold) (> right threshold))
(LOG "Line: LEFT"))
((AND (> left threshold) (< center threshold) (> right threshold))
(LOG "Line: CENTER"))
((AND (> left threshold) (> center threshold) (< right threshold))
(LOG "Line: RIGHT"))
((LOG "Line: LOST")))))

PID Line Following​

; PID parameters
(SET kp 2.0)
(SET ki 0.0)
(SET kd 0.5)
(SET last-error 0)
(SET integral 0)

(DEFUN LINE-FOLLOW ()
(DO
(SET error (- (READ A 0) (READ A 2)))
(SET integral (+ integral error))
(SET derivative (- error last-error))
(SET correction (+ (* kp error) (* ki integral) (* kd derivative)))
(SET last-error error)
correction))

; Main loop
(WHILE 1
(DO
(SET corr (LINE-FOLLOW))
(SET left-speed (CONSTRAIN (+ 200 corr) 0 255))
(SET right-speed (CONSTRAIN (- 200 corr) 0 255))
(WRITE P 5 left-speed)
(WRITE P 6 right-speed)))

Remote Control​

; Simple Bluetooth remote control
; Commands: F=forward, B=backward, L=left, R=right, S=stop

(WHILE 1
(DO
(SET cmd (UART-READ))
(COND
((== cmd "F") (FORWARD))
((== cmd "B") (BACKWARD))
((== cmd "L") (TURN-LEFT))
((== cmd "R") (TURN-RIGHT))
((== cmd "S") (STOP)))))

References

Siciliano, B., & Khatib, O. (Eds.). (2016). Springer handbook of robotics (2nd ed.). Springer.