Example 8 using abs

Simulation

not found

Real Life

not found

What does the code do?

In this code the robot moves forward until abs < distance, then drives back to original position.

Functions Used

cmpc( clear_motor_position_counter)

abs( absolute )

gmpc( get_motor_position_counter)

mav( motor port, velocity)

msleep( distance in ms)

Function Implementation

 1#!/usr/bin/python3
 2import os, sys
 3sys.path.append("/usr/lib")
 4from kipr import*
 5
 6def drive(distance, speed):
 7       cmpc(0)
 8       while abs(gmpc(0)) < distance:
 9           mav(0, speed)
10           mav(3, speed)
11       mav(0, 0)
12       mav(3, 0)
13       msleep(20)
14
15def main():
16       drive(1000, 500)
17       drive(1000, -300)
18
19if __name__ == "__main__":
20   main()

Rookie Tips

You can make multiple define(func) in a code inorder to make your code more organised!