![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Random walk for psiSwarm
Dependencies: PsiSwarmV8_CPP mbed
Fork of PsiSwarm_V8_Blank_CPP by
main.cpp@3:5d11c69d95df, 2017-08-03 (annotated)
- Committer:
- alixander
- Date:
- Thu Aug 03 13:00:02 2017 +0000
- Revision:
- 3:5d11c69d95df
- Parent:
- 2:5d37e5fd7141
Random walk for PsiSwarm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 0:9c0ccd879613 | 1 | /*********************************************************************** |
jah128 | 0:9c0ccd879613 | 2 | ** ██████╗ ███████╗██╗███████╗██╗ ██╗ █████╗ ██████╗ ███╗ ███╗ ** |
jah128 | 0:9c0ccd879613 | 3 | ** ██╔══██╗██╔════╝██║██╔════╝██║ ██║██╔══██╗██╔══██╗████╗ ████║ ** |
jah128 | 0:9c0ccd879613 | 4 | ** ██████╔╝███████╗██║███████╗██║ █╗ ██║███████║██████╔╝██╔████╔██║ ** |
jah128 | 0:9c0ccd879613 | 5 | ** ██╔═══╝ ╚════██║██║╚════██║██║███╗██║██╔══██║██╔══██╗██║╚██╔╝██║ ** |
jah128 | 0:9c0ccd879613 | 6 | ** ██║ ███████║██║███████║╚███╔███╔╝██║ ██║██║ ██║██║ ╚═╝ ██║ ** |
jah128 | 0:9c0ccd879613 | 7 | ** ╚═╝ ╚══════╝╚═╝╚══════╝ ╚══╝╚══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ** |
jah128 | 0:9c0ccd879613 | 8 | ************************************************************************ |
jah128 | 2:5d37e5fd7141 | 9 | ** Copyright 2016 University of York - See notice at end of file ** |
jah128 | 0:9c0ccd879613 | 10 | ***********************************************************************/ |
jah128 | 0:9c0ccd879613 | 11 | |
jah128 | 2:5d37e5fd7141 | 12 | /// PsiSwarm C++ Blank Example Code - Version 0.8 |
jah128 | 0:9c0ccd879613 | 13 | /// James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis |
jah128 | 0:9c0ccd879613 | 14 | |
jah128 | 0:9c0ccd879613 | 15 | /// Include main.h - this includes psiswarm.h all the other necessary core files |
jah128 | 0:9c0ccd879613 | 16 | #include "main.h" |
alixander | 3:5d11c69d95df | 17 | #include <cstdlib> |
jah128 | 0:9c0ccd879613 | 18 | |
jah128 | 2:5d37e5fd7141 | 19 | Psiswarm psi; |
jah128 | 2:5d37e5fd7141 | 20 | |
jah128 | 0:9c0ccd879613 | 21 | char * program_name = "Blank"; |
jah128 | 0:9c0ccd879613 | 22 | char * author_name = "YRL"; |
jah128 | 2:5d37e5fd7141 | 23 | char * version_name = "0.80"; |
jah128 | 0:9c0ccd879613 | 24 | |
alixander | 3:5d11c69d95df | 25 | Timer t; //set a timer |
alixander | 3:5d11c69d95df | 26 | int how_long; |
alixander | 3:5d11c69d95df | 27 | |
jah128 | 2:5d37e5fd7141 | 28 | ///User code loop: This is where user code should go; it is run as an infinite loop |
jah128 | 0:9c0ccd879613 | 29 | void user_code_loop() |
jah128 | 0:9c0ccd879613 | 30 | { |
alixander | 3:5d11c69d95df | 31 | motors.forward(0.2); //simple code, the psiswarm continues forwards |
alixander | 3:5d11c69d95df | 32 | led.set_leds(0b10101010,0b01010101); //illuminate circumference with alternating led pattern |
alixander | 3:5d11c69d95df | 33 | |
alixander | 3:5d11c69d95df | 34 | t.start(); |
alixander | 3:5d11c69d95df | 35 | how_long = t.read(); |
alixander | 3:5d11c69d95df | 36 | |
alixander | 3:5d11c69d95df | 37 | if(how_long > 1) { |
alixander | 3:5d11c69d95df | 38 | float turn = (rand()%6)/10.0f - 0.25; |
alixander | 3:5d11c69d95df | 39 | //float turn = |
alixander | 3:5d11c69d95df | 40 | |
alixander | 3:5d11c69d95df | 41 | char s1[17]; |
alixander | 3:5d11c69d95df | 42 | |
alixander | 3:5d11c69d95df | 43 | sprintf(s1,"%.3f",turn); |
alixander | 3:5d11c69d95df | 44 | display.clear_display(); |
alixander | 3:5d11c69d95df | 45 | display.set_position(0,0); |
alixander | 3:5d11c69d95df | 46 | display.write_string(s1); |
alixander | 3:5d11c69d95df | 47 | |
alixander | 3:5d11c69d95df | 48 | motors.turn(turn); |
alixander | 3:5d11c69d95df | 49 | wait(1); |
alixander | 3:5d11c69d95df | 50 | t.reset(); |
alixander | 3:5d11c69d95df | 51 | } |
alixander | 3:5d11c69d95df | 52 | |
jah128 | 0:9c0ccd879613 | 53 | } |
jah128 | 0:9c0ccd879613 | 54 | |
jah128 | 0:9c0ccd879613 | 55 | ///Place user code here that should be run after initialisation but before the main loop |
jah128 | 0:9c0ccd879613 | 56 | void user_code_setup() |
jah128 | 0:9c0ccd879613 | 57 | { |
jah128 | 0:9c0ccd879613 | 58 | wait(1); |
jah128 | 0:9c0ccd879613 | 59 | display.clear_display(); |
jah128 | 0:9c0ccd879613 | 60 | display.set_position(0,0); |
alixander | 3:5d11c69d95df | 61 | display.write_string("Going for a walk..."); //display 'Going for a walk...' to indicate robot's function |
jah128 | 0:9c0ccd879613 | 62 | } |
jah128 | 0:9c0ccd879613 | 63 | |
jah128 | 0:9c0ccd879613 | 64 | /// Code goes here to handle what should happen when the user switch is pressed |
jah128 | 0:9c0ccd879613 | 65 | void handle_switch_event(char switch_state) |
jah128 | 0:9c0ccd879613 | 66 | { |
jah128 | 0:9c0ccd879613 | 67 | /// Switch_state = 1 if up is pressed, 2 if down is pressed, 4 if left is pressed, 8 if right is pressed and 16 if the center button is pressed |
jah128 | 0:9c0ccd879613 | 68 | /// NB For maximum compatability it is recommended to minimise reliance on center button press |
jah128 | 0:9c0ccd879613 | 69 | } |
jah128 | 0:9c0ccd879613 | 70 | |
jah128 | 0:9c0ccd879613 | 71 | void handle_user_serial_message(char * message, char length, char interface) |
jah128 | 0:9c0ccd879613 | 72 | { |
jah128 | 0:9c0ccd879613 | 73 | // This is where user code for handling a (non-system) serial message should go |
jah128 | 0:9c0ccd879613 | 74 | // |
jah128 | 0:9c0ccd879613 | 75 | // message = pointer to message char array |
jah128 | 0:9c0ccd879613 | 76 | // length = length of message |
jah128 | 0:9c0ccd879613 | 77 | // interface = 0 for PC serial connection, 1 for Bluetooth |
jah128 | 0:9c0ccd879613 | 78 | } |
jah128 | 0:9c0ccd879613 | 79 | |
jah128 | 0:9c0ccd879613 | 80 | /// The main routine: it is recommended to leave this function alone and add user code to the above functions |
jah128 | 0:9c0ccd879613 | 81 | int main() |
jah128 | 0:9c0ccd879613 | 82 | { |
jah128 | 2:5d37e5fd7141 | 83 | psi.init(); ///psi.init() in psiswarm.cpp sets up the robot |
jah128 | 2:5d37e5fd7141 | 84 | user_code_setup(); ///run user code setup block |
jah128 | 2:5d37e5fd7141 | 85 | user_code_running = 1; ///nb. user code can be paused by external commands sent from PC\BT interfaces |
jah128 | 0:9c0ccd879613 | 86 | while(1) { |
jah128 | 2:5d37e5fd7141 | 87 | user_code_loop(); ///run user code |
jah128 | 0:9c0ccd879613 | 88 | } |
jah128 | 2:5d37e5fd7141 | 89 | } |
jah128 | 2:5d37e5fd7141 | 90 | |
jah128 | 2:5d37e5fd7141 | 91 | |
jah128 | 2:5d37e5fd7141 | 92 | /*********************************************************************** |
jah128 | 2:5d37e5fd7141 | 93 | ** Copyright 2016 University of York ** |
jah128 | 2:5d37e5fd7141 | 94 | ** ** |
jah128 | 2:5d37e5fd7141 | 95 | ** Licensed under the Apache License, Version 2.0 (the "License") ** |
jah128 | 2:5d37e5fd7141 | 96 | ** You may not use this file except in compliance with the License. ** |
jah128 | 2:5d37e5fd7141 | 97 | ** You may obtain a copy of the License at ** |
jah128 | 2:5d37e5fd7141 | 98 | ** http://www.apache.org/licenses/LICENSE-2.0 Unless required by ** |
jah128 | 2:5d37e5fd7141 | 99 | ** applicable law or agreed to in writing, software distributed under ** |
jah128 | 2:5d37e5fd7141 | 100 | ** under the License is distributed on an "AS IS" BASIS WITHOUT ** |
jah128 | 2:5d37e5fd7141 | 101 | ** WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** |
jah128 | 2:5d37e5fd7141 | 102 | ** See the License for the specific language governing permissions ** |
jah128 | 2:5d37e5fd7141 | 103 | ** and limitations under the License. ** |
jah128 | 2:5d37e5fd7141 | 104 | ***********************************************************************/ |