YRL Maze lab made more script-y

Dependencies:   PsiSwarmLab-ScriptingBased mbed

Fork of UKESF_Lab by UKESF Headstart Summer School

Committer:
YRL50
Date:
Fri Sep 14 16:01:22 2018 +0000
Revision:
32:cdcc91651e55
Parent:
31:7fa2c47d73a2
Child:
34:232559052b2f
Initial commit of the change to a more script based approach for easier lab scripts

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 6:ff3c66f7372b 4
jah128 6:ff3c66f7372b 5 #ifndef MAIN_H
jah128 6:ff3c66f7372b 6 #define MAIN_H
jah128 6:ff3c66f7372b 7
jah128 10:1b09d4bb847b 8 #include "psiswarm.h"
jah128 9:085e090e1ec1 9
jah128 30:513457c1ad12 10 int main(void);
jah128 30:513457c1ad12 11 void handle_switch_event(char switch_state);
jah128 30:513457c1ad12 12 void handle_user_serial_message(char * message, char length, char interface);
YRL50 32:cdcc91651e55 13
YRL50 32:cdcc91651e55 14 static float left_speeds[2] = {0.0, 0.0};
YRL50 32:cdcc91651e55 15 static float right_speeds[2] = {0.0, 0.0};
YRL50 32:cdcc91651e55 16 static float forward_speeds[2] = {0.0, 0.0};
YRL50 32:cdcc91651e55 17
YRL50 32:cdcc91651e55 18 void inline set_left_turn_speeds(float l, float r) { left_speeds[0] = l; left_speeds[1] = r; }
YRL50 32:cdcc91651e55 19 void inline set_right_turn_speeds(float l, float r) { right_speeds[0] = l; right_speeds[1] = r; }
YRL50 32:cdcc91651e55 20 void inline set_forward_wheel_speeds(float l, float r) { forward_speeds[0] = l; forward_speeds[1] = r; }
YRL50 32:cdcc91651e55 21
YRL50 32:cdcc91651e55 22 static float cm_in_5_seconds = 40;
YRL50 32:cdcc91651e55 23 static float degrees_in_5_seconds = 300;
YRL50 32:cdcc91651e55 24
YRL50 32:cdcc91651e55 25 void inline set_distance_in_5_seconds(float dist) { cm_in_5_seconds = dist; }
YRL50 32:cdcc91651e55 26 void inline set_degrees_in_5_seconds(float degrees) { degrees_in_5_seconds = degrees; }
YRL50 32:cdcc91651e55 27
YRL50 32:cdcc91651e55 28 inline void forward_timed(int seconds)
YRL50 32:cdcc91651e55 29 {
YRL50 32:cdcc91651e55 30 set_motor_speed(forward_speeds[0], forward_speeds[1]);
YRL50 32:cdcc91651e55 31 wait(seconds);
YRL50 32:cdcc91651e55 32 brake();
YRL50 32:cdcc91651e55 33 }
YRL50 32:cdcc91651e55 34
YRL50 32:cdcc91651e55 35 inline void forward_cm(int distance_in_cm)
YRL50 32:cdcc91651e55 36 {
YRL50 32:cdcc91651e55 37 float cm_in_1_second = cm_in_5_seconds/5;
YRL50 32:cdcc91651e55 38 float seconds_for_1_cm = 1/cm_in_1_second;
YRL50 32:cdcc91651e55 39 float seconds_to_move = seconds_for_1_cm * distance_in_cm;
YRL50 32:cdcc91651e55 40
YRL50 32:cdcc91651e55 41 forward_timed(seconds_to_move);
YRL50 32:cdcc91651e55 42 }
YRL50 32:cdcc91651e55 43
YRL50 32:cdcc91651e55 44
YRL50 32:cdcc91651e55 45 inline void turn_right_timed(float turn_time)
YRL50 32:cdcc91651e55 46 {
YRL50 32:cdcc91651e55 47 set_motor_speed(right_speeds[0], right_speeds[1]);
YRL50 32:cdcc91651e55 48 wait(turn_time);
YRL50 32:cdcc91651e55 49 brake();
YRL50 32:cdcc91651e55 50 }
YRL50 32:cdcc91651e55 51
YRL50 32:cdcc91651e55 52 inline void turn_right_degrees(float degrees)
YRL50 32:cdcc91651e55 53 {
YRL50 32:cdcc91651e55 54 float degrees_in_1_second = degrees_in_5_seconds/5;
YRL50 32:cdcc91651e55 55 float seconds_per_degree = 1/degrees_in_1_second;
YRL50 32:cdcc91651e55 56 float turn_time = seconds_per_degree * degrees;
YRL50 32:cdcc91651e55 57
YRL50 32:cdcc91651e55 58 turn_right_timed(turn_time);
YRL50 32:cdcc91651e55 59 }
YRL50 32:cdcc91651e55 60
YRL50 32:cdcc91651e55 61 inline void turn_left_timed(float turn_time)
YRL50 32:cdcc91651e55 62 {
YRL50 32:cdcc91651e55 63 set_motor_speed(left_speeds[0], left_speeds[1]);
YRL50 32:cdcc91651e55 64 wait(turn_time);
YRL50 32:cdcc91651e55 65 brake();
YRL50 32:cdcc91651e55 66 }
YRL50 32:cdcc91651e55 67
YRL50 32:cdcc91651e55 68 inline void turn_left_degrees(float degrees)
YRL50 32:cdcc91651e55 69 {
YRL50 32:cdcc91651e55 70 float degrees_in_1_second = degrees_in_5_seconds/5;
YRL50 32:cdcc91651e55 71 float seconds_per_degree = 1/degrees_in_1_second;
YRL50 32:cdcc91651e55 72 float turn_time = seconds_per_degree * degrees;
YRL50 32:cdcc91651e55 73
YRL50 32:cdcc91651e55 74 turn_left_timed(turn_time);
YRL50 32:cdcc91651e55 75 }
jah128 6:ff3c66f7372b 76
jah128 6:ff3c66f7372b 77 #endif