Psi Swarm robot library version 0.9

Dependents:   PsiSwarm_V9_Blank

Fork of PsiSwarmV9 by James Hilder

Committer:
richardredpath
Date:
Mon Jul 08 10:50:40 2019 +0000
Revision:
20:2b6ebe60929d
Parent:
19:3e3b03d80ea3
Fixed deprecated warnings for callbacks throughout the library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 12:878c6e9d9e60 1 /* University of York Robotics Laboratory PsiSwarm Library: Animations Source File
jah128 6:b340a527add9 2 *
jah128 14:2f1ad77d281e 3 * Copyright 2017 University of York
jah128 6:b340a527add9 4 *
jah128 6:b340a527add9 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
jah128 6:b340a527add9 6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
jah128 6:b340a527add9 7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS
jah128 6:b340a527add9 8 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jah128 6:b340a527add9 9 * See the License for the specific language governing permissions and limitations under the License.
jah128 0:d6269d17c8cf 10 *
jah128 12:878c6e9d9e60 11 * Library of simple predetermined movements and LED animations
jah128 0:d6269d17c8cf 12 *
jah128 12:878c6e9d9e60 13 * File: animations.cpp
jah128 12:878c6e9d9e60 14 * [Was dances.cpp in version 0.7]
jah128 0:d6269d17c8cf 15 *
jah128 0:d6269d17c8cf 16 * (C) Dept. Electronics & Computer Science, University of York
jah128 0:d6269d17c8cf 17 * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
jah128 0:d6269d17c8cf 18 *
jah128 14:2f1ad77d281e 19 * PsiSwarm Library Version: 0.9
jah128 0:d6269d17c8cf 20 *
jah128 14:2f1ad77d281e 21 * June 2017
jah128 0:d6269d17c8cf 22 *
jah128 0:d6269d17c8cf 23 *
jah128 0:d6269d17c8cf 24 */
jah128 0:d6269d17c8cf 25
jah128 0:d6269d17c8cf 26
jah128 0:d6269d17c8cf 27 #include "psiswarm.h"
jah128 0:d6269d17c8cf 28
jah128 12:878c6e9d9e60 29 char hold_colour = 1;
jah128 19:3e3b03d80ea3 30 char animation_running = 0;
jah128 12:878c6e9d9e60 31 //
jah128 12:878c6e9d9e60 32 void Animations::set_colour(char colour)
jah128 12:878c6e9d9e60 33 {
jah128 12:878c6e9d9e60 34 hold_colour = colour;
jah128 12:878c6e9d9e60 35 }
jah128 12:878c6e9d9e60 36
jah128 19:3e3b03d80ea3 37 //Blink through LED pattern
jah128 19:3e3b03d80ea3 38 void Animations::led_run1(void)
jah128 19:3e3b03d80ea3 39 {
jah128 19:3e3b03d80ea3 40 if(animation_running == 0){
jah128 19:3e3b03d80ea3 41 animation_counter = 0;
jah128 19:3e3b03d80ea3 42 animation_running = 1;
jah128 19:3e3b03d80ea3 43 IF_led_run1();
jah128 19:3e3b03d80ea3 44 }else{
jah128 19:3e3b03d80ea3 45 psi.debug("WARNING: Animation called before previous animation finished, ignoring\n");
jah128 19:3e3b03d80ea3 46 }
jah128 19:3e3b03d80ea3 47 }
jah128 19:3e3b03d80ea3 48
jah128 19:3e3b03d80ea3 49 void Animations::IF_led_run1()
jah128 12:878c6e9d9e60 50 {
jah128 12:878c6e9d9e60 51 if(animation_counter == 0)led.save_led_states();
jah128 12:878c6e9d9e60 52 char led_pattern = 0x0;
jah128 12:878c6e9d9e60 53 if(animation_counter < 16){
jah128 12:878c6e9d9e60 54 switch(animation_counter % 4){
jah128 12:878c6e9d9e60 55 case 0:
jah128 12:878c6e9d9e60 56 led_pattern = 0x10;
jah128 12:878c6e9d9e60 57 break;
jah128 12:878c6e9d9e60 58 case 1:
jah128 12:878c6e9d9e60 59 led_pattern = 0x28;
jah128 12:878c6e9d9e60 60 break;
jah128 12:878c6e9d9e60 61 case 2:
jah128 12:878c6e9d9e60 62 led_pattern = 0x44;
jah128 12:878c6e9d9e60 63 break;
jah128 12:878c6e9d9e60 64 case 3:
jah128 12:878c6e9d9e60 65 led_pattern = 0x83;
jah128 12:878c6e9d9e60 66 break;
jah128 12:878c6e9d9e60 67 }
jah128 12:878c6e9d9e60 68 }
jah128 12:878c6e9d9e60 69 if(animation_counter == 16 || animation_counter == 18 || animation_counter == 20) led_pattern = 0x01;
jah128 12:878c6e9d9e60 70 char green_state = 0;
jah128 12:878c6e9d9e60 71 char red_state = 0;
jah128 12:878c6e9d9e60 72 if(hold_colour % 2 == 1) red_state = led_pattern;
jah128 12:878c6e9d9e60 73 if(hold_colour > 1) green_state = led_pattern;
jah128 12:878c6e9d9e60 74 led.set_leds(green_state,red_state);
jah128 12:878c6e9d9e60 75 animation_counter++;
jah128 12:878c6e9d9e60 76 if(animation_counter < 21) {
richardredpath 20:2b6ebe60929d 77 animation_timeout.attach(callback(this, &Animations::IF_led_run1), 0.05f);
jah128 12:878c6e9d9e60 78 } else {
jah128 12:878c6e9d9e60 79 animation_counter = 0;
jah128 12:878c6e9d9e60 80 led.restore_led_states();
jah128 19:3e3b03d80ea3 81 animation_running = 0;
jah128 12:878c6e9d9e60 82 }
jah128 12:878c6e9d9e60 83 }
jah128 0:d6269d17c8cf 84
jah128 11:312663037b8c 85 //Do a simple wiggle
jah128 12:878c6e9d9e60 86 void Animations::vibrate(void)
jah128 0:d6269d17c8cf 87 {
jah128 19:3e3b03d80ea3 88 if(animation_running == 0){
jah128 19:3e3b03d80ea3 89 animation_counter = 0;
jah128 19:3e3b03d80ea3 90 animation_running = 1;
jah128 19:3e3b03d80ea3 91 IF_vibrate();
jah128 19:3e3b03d80ea3 92 }else{
jah128 19:3e3b03d80ea3 93 psi.debug("WARNING: Animation called before previous animation finished, ignoring\n");
jah128 19:3e3b03d80ea3 94 }
jah128 19:3e3b03d80ea3 95 }
jah128 19:3e3b03d80ea3 96
jah128 19:3e3b03d80ea3 97 void Animations::IF_vibrate(void)
jah128 19:3e3b03d80ea3 98 {
jah128 12:878c6e9d9e60 99 if(animation_counter == 0)led.save_led_states();
jah128 12:878c6e9d9e60 100 if(animation_counter % 2 == 0) {
jah128 9:dde9e21030eb 101 led.set_leds(0xC7,0x00);
jah128 8:6c92789d5f87 102 motors.turn(1.0);
jah128 0:d6269d17c8cf 103 } else {
jah128 9:dde9e21030eb 104 led.set_leds(0x00,0xC7);
jah128 8:6c92789d5f87 105 motors.turn(-1.0);
jah128 0:d6269d17c8cf 106 }
jah128 12:878c6e9d9e60 107 animation_counter++;
jah128 0:d6269d17c8cf 108
jah128 12:878c6e9d9e60 109 if(animation_counter < 14) {
jah128 0:d6269d17c8cf 110 float wiggle_timeout_period = 0.06;
jah128 0:d6269d17c8cf 111 //Move less on first 'wiggle' so that we stay in roughly the same place!
jah128 12:878c6e9d9e60 112 if(animation_counter == 0) wiggle_timeout_period = 0.03;
richardredpath 20:2b6ebe60929d 113 animation_timeout.attach(callback(this, &Animations::IF_vibrate), wiggle_timeout_period);
jah128 0:d6269d17c8cf 114 } else {
jah128 12:878c6e9d9e60 115 animation_counter = 0;
jah128 8:6c92789d5f87 116 motors.brake();
jah128 9:dde9e21030eb 117 led.restore_led_states();
jah128 19:3e3b03d80ea3 118 animation_running = 0;
jah128 0:d6269d17c8cf 119 }
jah128 0:d6269d17c8cf 120 }