|
| 1 | +------------------------------------------------------------------------------------------------------- |
| 2 | +Page-1 : Robot Simulator |
| 3 | +------------------------------------------------------------------------------------------------------- |
| 4 | +This guide is for creating own robot simulator . |
| 5 | + |
| 6 | + |
| 7 | +DISCLAIMER |
| 8 | +This is a small initive for steping towards AI , use of most widly popular computer language PYTHON for everybody who are involve in robotics in our country and are just wandering with embedded system and mechanics . |
| 9 | + |
| 10 | +I hope to colaborate with you |
| 11 | +Santosh Dahal |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +What is robot simulator ? |
| 16 | + |
| 17 | +The digital version of your robot iniside computer , where you can operate , test your algorithm before using on real robot . |
| 18 | + |
| 19 | +What are benefits of own robot simulator ? |
| 20 | + |
| 21 | +There are other simulator around like ROS , V-REP. They consist of high level of abstraction and own hardware which are expensive . By using this simulator you can make your robot intelligence with help of your own computer. |
| 22 | + |
| 23 | +This course is designed as a guide for all the robotics enthugist outside there who are interested for making their robot intelligence and get yourself updated with robotics trends and technology. |
| 24 | + |
| 25 | +------------------------------------------------------------------------------------------------------------- |
| 26 | +Page-2 : GUI application |
| 27 | +------------------------------------------------------------------------------------------------------------- |
| 28 | + |
| 29 | +Simulator is same as other GUI application with algorithms used in robotics and contain diffrerent robotic model in which you can play around. |
| 30 | + |
| 31 | +So ,first we will follow the procedure for creating simple GUI application. |
| 32 | + |
| 33 | +1) Opening a window |
| 34 | + |
| 35 | + import pygame |
| 36 | + |
| 37 | + pygame.init() |
| 38 | + |
| 39 | + screen = pygame.display.set_mode((640,480)) |
| 40 | + |
| 41 | +First of all, to use pygame we have to import it. Then we create a Surface that is 640 pixels wide and 400 pixels high. There are a lot more things you can do with set_mode, but we will keep it simple at this point. |
| 42 | + |
| 43 | +This code give you the window open for limited period of time.We have to add inifinite loop so the program never gets close without user wish. |
| 44 | + |
| 45 | + |
| 46 | +2) Break to exit |
| 47 | + |
| 48 | + |
| 49 | + running = True |
| 50 | + while running: |
| 51 | + for event in pygame.event.get(): |
| 52 | + if event.type == pygame.QUIT: |
| 53 | + running = false |
| 54 | + if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: |
| 55 | + running = flase |
| 56 | + |
| 57 | + |
| 58 | +The loop is controlled by a flag called running. As long as the flag is set the loop keeps running. Inside the loop we use the get() method to grab the event that occur since next execution. |
| 59 | + |
| 60 | +There are several different event types that pygame knows about and can respond to. One of these is QUIT and KEYDOWN |
| 61 | +QUIT : Gets triggered when the user clicks on the window’s close button. |
| 62 | + |
| 63 | +KEYDOWN : Gets triggered when the user any keyboard button.When we recieve a KEYDOWN event, the event object will hold the code of the key that was pressed in an attribute called ‘key’. So by comparing event.key to whichever key we are interested in, we can find out if that was the actual key that was pressed. |
| 64 | + |
| 65 | +All we do if we get an event of this type is clear the running flag, which will exit the loop and cause the program to terminate. Still simple, isn’t it. |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +3) Structuring Program |
| 71 | + |
| 72 | +import pygame |
| 73 | + |
| 74 | +class Simulator(object): |
| 75 | + def main(self , screen): |
| 76 | + while 1: |
| 77 | + for event in pygame.event.get(): |
| 78 | + if event.type == pygame.QUIT: |
| 79 | + return |
| 80 | + if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: |
| 81 | + return |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + pygame.init() |
| 87 | + screen = pygame.display.set_mode((640,480)) |
| 88 | + Simulator().main(screen) |
| 89 | + |
| 90 | +We have clean our code and apply OOP concept . If you are not familiar with OOP , please prepare yourself about class , object .How to initilize and create them. |
| 91 | + |
| 92 | +-------------- |
| 93 | +EXECRISE: |
| 94 | +-------------- |
| 95 | +1. Create a window that is 320 pixels wide and 200 pixels high. |
| 96 | +2. Create a program where the user can specify the width and the height as command line arguments. |
| 97 | +3. Create a program that asks the users for the width and the height and then displays the window. |
| 98 | +4. Write a program that calls pygame.display.set_mode twice with different sizes. What do you expect should happen? Run the program. What actually happens? Can you explain why? |
| 99 | + |
| 100 | +------------------------------------------------------------------------------------------------------------ |
| 101 | +page-3 : Drawing into Screen |
| 102 | +------------------------------------------------------------------------------------------------------------- |
| 103 | +4) Drawing into Screen |
| 104 | + |
| 105 | +import pygame |
| 106 | + |
| 107 | +class Simulator(object): |
| 108 | + def main(self , screen): |
| 109 | + robot = pygame.image.load("car60_40.png") |
| 110 | + |
| 111 | + while 1: |
| 112 | + for event in pygame.event.get(): |
| 113 | + if event.type == pygame.QUIT: |
| 114 | + return |
| 115 | + if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: |
| 116 | + return |
| 117 | + screen.blit(robot,(320,240)) |
| 118 | + pygame.display.flip() |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + pygame.init() |
| 123 | + screen = pygame.display.set_mode((640,480)) |
| 124 | + Simulator().main(screen) |
| 125 | + |
| 126 | +The next thing that is new is that we call pygame.display.flip. Drawing directly to the screen is usually a very bad idea. Instead, we have a invisible buffer that we draw onto. When we have finished drawing we make the buffer visible. That way we get flicker-free animations |
| 127 | + |
| 128 | + |
| 129 | +5) Set Background Color |
| 130 | + |
| 131 | +red = (200, 0, 0) |
| 132 | +blue = (0, 0, 255) |
| 133 | +green = (0, 155, 0) |
| 134 | +yellow = (155, 155, 0) |
| 135 | +white = (255, 255, 255) |
| 136 | +black = (0, 0, 0) |
| 137 | + |
| 138 | +screen.fill(white) |
| 139 | + |
| 140 | +We have passed in a sequence of three values: red, green and blue. Each one can be a value between 0 and 255. Since we set all to zero, we get a black screen. You should experiment with different values for red, green and blue. |
| 141 | + |
| 142 | + |
| 143 | +6) Using Less CPU |
| 144 | + |
| 145 | +clock = pygame.time.Clock() |
| 146 | +clock.tick(30) |
| 147 | + |
| 148 | + |
| 149 | +-------------- |
| 150 | +EXECRISE: |
| 151 | +-------------- |
| 152 | +1. Create a window with a white background color. |
| 153 | +2. Create a window with a red background color. |
| 154 | +3. Experiment with setting different background colors. If you are not familiar with RGB values then spend a little extra time to figure out how to get colors like yellow, brown, cyan etc. |
| 155 | +4. Create a program that asks the user to specify the values for red, green and blue. Check that the values are in the valid range (0-255) and then use these for the background color. |
| 156 | +5.Create a program that upon start-up checks the time of the day and sets the brightness of the background color accordingly. Use a blue scale. If it is midnight, the screen should be black. If it is midday, the screen should be bright blue. Any other time the background should be something in between corresponding to the brightness outside. |
| 157 | + |
| 158 | + |
| 159 | +-------------------------------------------------------------------------------------------------------------- |
| 160 | +page-4 : Moving things |
| 161 | +-------------------------------------------------------------------------------------------------------------- |
| 162 | + |
| 163 | +7) Moving along x |
| 164 | + |
| 165 | +import pygame |
| 166 | + |
| 167 | +"""Define color""" |
| 168 | +red = (200, 0, 0) |
| 169 | +blue = (0, 0, 255) |
| 170 | +green = (0, 155, 0) |
| 171 | +yellow = (155, 155, 0) |
| 172 | +white = (255, 255, 255) |
| 173 | +black = (0, 0, 0) |
| 174 | + |
| 175 | +class Simulator(object): |
| 176 | + def main(self , screen): |
| 177 | + clock = pygame.time.Clock() |
| 178 | + robot = pygame.image.load("car60_40.png") |
| 179 | + robot_x = 320 |
| 180 | + robot_y = 240 |
| 181 | + while 1: |
| 182 | + clock.tick(30) |
| 183 | + for event in pygame.event.get(): |
| 184 | + if event.type == pygame.QUIT: |
| 185 | + return |
| 186 | + if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: |
| 187 | + return |
| 188 | + screen.fill(white) |
| 189 | + robot_x += 1 |
| 190 | + screen.blit(robot,(robot_x,robot_y)) |
| 191 | + pygame.display.flip() |
| 192 | + |
| 193 | + |
| 194 | +if __name__ == '__main__': |
| 195 | + pygame.init() |
| 196 | + pygame.display.set_caption("Move") |
| 197 | + screen = pygame.display.set_mode((640,480)) |
| 198 | + Simulator().main(screen) |
| 199 | + |
| 200 | +We have separate variable for robot x & y position , so we can manipulate in runtime. |
| 201 | + |
| 202 | + |
| 203 | +8) Organizing Robot into class |
| 204 | + |
| 205 | +We will build class for mobile robot . Think what property of car is essential for imagining it in plane coordinate. |
| 206 | + |
| 207 | +We need x and y co-ordinate for it's position and angle for it's orientation |
| 208 | + |
| 209 | +class robot: |
| 210 | + def __init__(self): |
| 211 | + self.x = 0 |
| 212 | + self.y = 0 |
| 213 | + self.orientation = 0 |
| 214 | + |
| 215 | + def set(self, x, y,orientation): |
| 216 | + self.x = x |
| 217 | + self.y = y |
| 218 | + self.orientation = orientation |
| 219 | + |
| 220 | + def move(self, turn, x,y): |
| 221 | + self.orientation = self.orientation + turn |
| 222 | + self.x = self.x + x |
| 223 | + self.y = self.y - y |
| 224 | + |
| 225 | + def draw(self): |
| 226 | + car_img = pygame.image.load("car60_40.png") |
| 227 | + img = pygame.transform.rotate(car_img, self.orientation) |
| 228 | + screen.blit(img, (self.x, self.y)) |
| 229 | + |
| 230 | +class Simulator(object): |
| 231 | + def main(self , screen , robot): |
| 232 | + clock = pygame.time.Clock() |
| 233 | + robot.draw() |
| 234 | + x = 0 |
| 235 | + y = 0 |
| 236 | + orientation = 0 |
| 237 | + while 1: |
| 238 | + clock.tick(30) |
| 239 | + screen.fill(white) |
| 240 | + for event in pygame.event.get(): |
| 241 | + if event.type == pygame.QUIT: |
| 242 | + return |
| 243 | + if event.type == pygame.KEYDOWN: |
| 244 | + if event.key == pygame.K_LEFT: |
| 245 | + x = -1 |
| 246 | + elif event.key == pygame.K_RIGHT: |
| 247 | + x = 1 |
| 248 | + elif event.key == pygame.K_UP: |
| 249 | + y = 1 |
| 250 | + elif event.key == pygame.K_DOWN: |
| 251 | + y = -1 |
| 252 | + elif event.key == pygame.K_a: |
| 253 | + orientation = -1 |
| 254 | + elif event.key == pygame.K_d: |
| 255 | + orientation = 1 |
| 256 | + elif event.type == pygame.KEYUP: |
| 257 | + if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT or event.key == pygame.K_UP or event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_DOWN: |
| 258 | + x = 0 |
| 259 | + y = 0 |
| 260 | + orientation = 0 |
| 261 | + robot.move(orientation , x , y) |
| 262 | + robot.draw() |
| 263 | + pygame.display.flip() |
| 264 | + |
| 265 | + |
| 266 | +robot = robot() |
| 267 | +Simulator().main(screen , robot) |
| 268 | + |
| 269 | +------------------------------------------------------------------------------------------------------------- |
| 270 | +page-5 : Building Real Model |
| 271 | +------------------------------------------------------------------------------------------------------------- |
| 272 | + |
| 273 | +9) Building Real Model |
| 274 | + |
| 275 | +In real car does not move in such manner . We must apply some of the math we read in school to have work it like real life. |
| 276 | + |
| 277 | +We only apply orientation and accelaration as forward to the car . But how can we move our car in real manner see the code below, if you didn't get it contact me. |
| 278 | + |
| 279 | + def move(self, turn, forward): |
| 280 | + self.orientation = self.orientation + turn |
| 281 | + #cos value 0degree->1 , 90degree->0, 180degree->-1 |
| 282 | + #we must pass degree in radian |
| 283 | + self.x = self.x + forward*cos(self.orientation*pi/180) |
| 284 | + #sin value 0degree->0 , 90degree->1, 180degree->0 |
| 285 | + #we must pass degree in radian |
| 286 | + self.y = self.y - forward*sin(self.orientation*pi/180) |
| 287 | + |
| 288 | + |
| 289 | +10) Move within the boundary |
| 290 | + |
| 291 | + self.orientation %= 360 |
| 292 | + self.x %= world_size |
| 293 | + self.y %= world_size |
| 294 | + |
| 295 | +The above code simply bound robot inside window i.e robot will come out of opposite side if it crossover one of the side . |
0 commit comments