YRL Maze lab made more script-y

Dependencies:   PsiSwarmLab-ScriptingBased mbed

Fork of UKESF_Lab by UKESF Headstart Summer School

Committer:
jah128
Date:
Mon Jun 20 13:36:30 2016 +0000
Revision:
31:7fa2c47d73a2
Parent:
30:513457c1ad12
Child:
32:cdcc91651e55
PsiSwarm Headstart Lab

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 31:7fa2c47d73a2 1 /**********************************************************
jah128 31:7fa2c47d73a2 2 * UKESF Headstart Summer School - Robot Programming Lab *
jah128 31:7fa2c47d73a2 3 **********************************************************/
jah128 0:8a5497a2e366 4
jah128 31:7fa2c47d73a2 5 // Note: A line beginning with // is a 'comment' and is
jah128 31:7fa2c47d73a2 6 // ignored by the compiler [it is shown in green text in the IDE]
jah128 0:8a5497a2e366 7
jah128 31:7fa2c47d73a2 8 // Include main.h - this contains the code needed to setup
jah128 31:7fa2c47d73a2 9 // the Psi-Swarm robot and its hardware
jah128 10:1b09d4bb847b 10 #include "main.h"
jah128 9:085e090e1ec1 11
jah128 31:7fa2c47d73a2 12 // You can change the author name to saomething else if you want - just make sure it is no more than 16 characters long
jah128 10:1b09d4bb847b 13 char * author_name = "YRL";
jah128 0:8a5497a2e366 14
jah128 1:f6356cf1cefc 15 /// The main routine: it is recommended to leave this function alone and add user code to the above functions
jah128 6:ff3c66f7372b 16 int main()
jah128 6:ff3c66f7372b 17 {
jah128 31:7fa2c47d73a2 18 // init() in psiswarm.cpp sets up the robot - we need to run this line of code before anything else or the robot won't work.
jah128 0:8a5497a2e366 19 init();
jah128 31:7fa2c47d73a2 20
jah128 31:7fa2c47d73a2 21 // We shall create a for-loop to try to move the robot in a 50cm square.
jah128 31:7fa2c47d73a2 22 for(int i=0; i<4; i++){
jah128 31:7fa2c47d73a2 23
jah128 31:7fa2c47d73a2 24 // Move the robot forward 50cm
jah128 31:7fa2c47d73a2 25 move_forward(50);
jah128 31:7fa2c47d73a2 26
jah128 31:7fa2c47d73a2 27 // Wait one second
jah128 31:7fa2c47d73a2 28 wait(1.0);
jah128 31:7fa2c47d73a2 29
jah128 31:7fa2c47d73a2 30 // Turn the robot right 90 degrees
jah128 31:7fa2c47d73a2 31 turn_right();
jah128 31:7fa2c47d73a2 32
jah128 31:7fa2c47d73a2 33 // Wait for one second
jah128 31:7fa2c47d73a2 34 wait(1.0);
jah128 0:8a5497a2e366 35 }
jah128 31:7fa2c47d73a2 36
jah128 31:7fa2c47d73a2 37 // When we have finished our code, we want to leave the MBED in an endless loop.
jah128 31:7fa2c47d73a2 38 while(1){}
jah128 31:7fa2c47d73a2 39 }
jah128 31:7fa2c47d73a2 40
jah128 31:7fa2c47d73a2 41 /**
jah128 31:7fa2c47d73a2 42 * void move_forward(int distance)
jah128 31:7fa2c47d73a2 43 * This function should move the robot forward for the distance
jah128 31:7fa2c47d73a2 44 * (in centimeter) specified by distance
jah128 31:7fa2c47d73a2 45 */
jah128 31:7fa2c47d73a2 46 void move_forward(int distance)
jah128 31:7fa2c47d73a2 47 {
jah128 31:7fa2c47d73a2 48 float left_motor_speed = 0.4;
jah128 31:7fa2c47d73a2 49 float right_motor_speed = 0.4;
jah128 31:7fa2c47d73a2 50 float cm_per_second = 25.0;
jah128 31:7fa2c47d73a2 51 float delay = distance / cm_per_second;
jah128 31:7fa2c47d73a2 52 set_motor_speed(left_motor_speed, right_motor_speed);
jah128 31:7fa2c47d73a2 53 wait(delay);
jah128 31:7fa2c47d73a2 54 brake();
jah128 31:7fa2c47d73a2 55 }
jah128 31:7fa2c47d73a2 56
jah128 31:7fa2c47d73a2 57 /**
jah128 31:7fa2c47d73a2 58 * void turn_right()
jah128 31:7fa2c47d73a2 59 * This function should turn the robot right by 90 degrees.
jah128 31:7fa2c47d73a2 60 */
jah128 31:7fa2c47d73a2 61 void turn_right()
jah128 31:7fa2c47d73a2 62 {
jah128 31:7fa2c47d73a2 63 float speed = 0.2;
jah128 31:7fa2c47d73a2 64 float degrees_per_second = 150.0;
jah128 31:7fa2c47d73a2 65 float delay = 90 / degrees_per_second;
jah128 31:7fa2c47d73a2 66 turn(speed);
jah128 31:7fa2c47d73a2 67 wait(delay);
jah128 31:7fa2c47d73a2 68 brake();
jah128 31:7fa2c47d73a2 69 }
jah128 31:7fa2c47d73a2 70
jah128 31:7fa2c47d73a2 71 /**
jah128 31:7fa2c47d73a2 72 * void turn_left()
jah128 31:7fa2c47d73a2 73 * This function should turn the robot left by 90 degrees. You will need to fill in the code here
jah128 31:7fa2c47d73a2 74 * HINT: You can use Ctrl-C and Ctrl-V to copy\paste blocks of code!
jah128 31:7fa2c47d73a2 75 */
jah128 31:7fa2c47d73a2 76 void turn_left()
jah128 31:7fa2c47d73a2 77 {
jah128 31:7fa2c47d73a2 78 }
jah128 31:7fa2c47d73a2 79
jah128 31:7fa2c47d73a2 80 ////////////////////////////////////////////////////////////////////////////////////////////////////
jah128 31:7fa2c47d73a2 81
jah128 31:7fa2c47d73a2 82 // You can ignore the code beyond this point: we need these variables and functions to make the code
jah128 31:7fa2c47d73a2 83 // compile without errors, but we aren't using the functions for this lab session.
jah128 31:7fa2c47d73a2 84 char * program_name = "Headstart";
jah128 31:7fa2c47d73a2 85 char * version_name = "0.5";
jah128 31:7fa2c47d73a2 86 void handle_switch_event(char switch_state){}
jah128 31:7fa2c47d73a2 87 void handle_user_serial_message(char * message, char length, char interface){}