2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
rsavitski
Date:
Sun Apr 14 22:02:21 2013 +0000
Revision:
70:0da6ca845762
Parent:
69:4b7bb92916da
Child:
74:9620d24a2f4e
Child:
76:532d9bc1d2aa
top flipper pushing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rsavitski 30:791739422122 1 #include "ai.h"
rsavitski 55:0c8897da6b3a 2 #include "rtos.h"
rsavitski 55:0c8897da6b3a 3 #include "globals.h"
rsavitski 55:0c8897da6b3a 4 #include "motion.h"
rsavitski 55:0c8897da6b3a 5 #include "Colour.h"
rsavitski 55:0c8897da6b3a 6 #include "supportfuncs.h"
rsavitski 65:4709ff6c753c 7 #include "Arm.h"
rsavitski 55:0c8897da6b3a 8
rsavitski 69:4b7bb92916da 9 //TODO: after 2013, kill entire AI layer as it is hacked together. Rest is fine-ish
rsavitski 30:791739422122 10
rsavitski 30:791739422122 11 namespace AI
rsavitski 30:791739422122 12 {
rsavitski 30:791739422122 13
rsavitski 69:4b7bb92916da 14 bool delayed_done = true; //TODO: kill
rsavitski 69:4b7bb92916da 15
rsavitski 69:4b7bb92916da 16 struct delayed_struct //TODO: kill
rsavitski 69:4b7bb92916da 17 {
rsavitski 69:4b7bb92916da 18 osThreadId tid;
rsavitski 69:4b7bb92916da 19 Waypoint *wpptr;
rsavitski 69:4b7bb92916da 20 };
rsavitski 69:4b7bb92916da 21
rsavitski 70:0da6ca845762 22 void arm_upper(const void *dummy); //TODO: kill
rsavitski 70:0da6ca845762 23
rsavitski 69:4b7bb92916da 24 void delayed_setter(const void *tid_wpptr); //TODO: kill the hack
rsavitski 69:4b7bb92916da 25
rsavitski 30:791739422122 26 void ailayer(void const *dummy)
rsavitski 30:791739422122 27 {
rsavitski 54:99d3158c9207 28 Waypoint current_waypoint;
rsavitski 69:4b7bb92916da 29
madcowswe 50:937e860f4621 30 Colour c_upper(P_COLOR_SENSOR_BLUE_UPPER, P_COLOR_SENSOR_RED_UPPER, P_COLOR_SENSOR_IN_UPPER, UPPER);
madcowswe 50:937e860f4621 31 Colour c_lower(P_COLOR_SENSOR_BLUE_LOWER, P_COLOR_SENSOR_RED_LOWER, P_COLOR_SENSOR_IN_LOWER, LOWER);
rsavitski 55:0c8897da6b3a 32
rsavitski 69:4b7bb92916da 33 // first waypoint for approach
rsavitski 54:99d3158c9207 34 current_waypoint.x = 2.2;
rsavitski 54:99d3158c9207 35 current_waypoint.y = 1.85;
madcowswe 67:be3ea5450cc7 36 current_waypoint.theta = (-3.0f/4.0f)*PI;
madcowswe 67:be3ea5450cc7 37 current_waypoint.pos_threshold = 0.03;
rsavitski 54:99d3158c9207 38 current_waypoint.angle_threshold = 0.02*PI;
rsavitski 69:4b7bb92916da 39
madcowswe 66:f1d75e51398d 40 motion::collavoiden = 1;
rsavitski 69:4b7bb92916da 41 motion::setNewWaypoint(Thread::gettid(),&current_waypoint);
rsavitski 55:0c8897da6b3a 42
rsavitski 68:662164480f60 43 float r = 0.61+0.02+0.01;
rsavitski 53:b013df99b747 44
madcowswe 66:f1d75e51398d 45 bool firstavoidstop = 1;
rsavitski 69:4b7bb92916da 46 delayed_struct ds = {Thread::gettid(),&current_waypoint};
rsavitski 70:0da6ca845762 47 RtosTimer delayed_wp_set(delayed_setter, osTimerOnce, (void *)&ds);
rsavitski 70:0da6ca845762 48 RtosTimer delayed_armer(arm_upper, osTimerOnce);
rsavitski 69:4b7bb92916da 49
rsavitski 54:99d3158c9207 50 for (float phi=(180-11.25)/180*PI; phi > 11.25/180*PI;)
rsavitski 30:791739422122 51 {
rsavitski 39:44d3dea4adcc 52 motion::waypoint_flag_mutex.lock();
rsavitski 69:4b7bb92916da 53 if (motion::checkMotionStatus() && (c_upper.getColour()==RED || firstavoidstop) && delayed_done)
rsavitski 30:791739422122 54 {
rsavitski 65:4709ff6c753c 55 //temphack!!!!!
rsavitski 69:4b7bb92916da 56 //Thread::wait(1000);
rsavitski 65:4709ff6c753c 57 arm::upper_arm.go_down();
rsavitski 69:4b7bb92916da 58 delayed_done = false;
rsavitski 70:0da6ca845762 59 delayed_armer.start(1200);
rsavitski 70:0da6ca845762 60 delayed_wp_set.start(2400);
rsavitski 69:4b7bb92916da 61 //Thread::wait(2000);
rsavitski 69:4b7bb92916da 62 //arm::upper_arm.go_up();
rsavitski 65:4709ff6c753c 63
rsavitski 54:99d3158c9207 64 phi -= 22.5/180*PI;
rsavitski 54:99d3158c9207 65 current_waypoint.x = 1.5-r*cos(phi);
rsavitski 54:99d3158c9207 66 current_waypoint.y = 2-r*sin(phi);
rsavitski 54:99d3158c9207 67 current_waypoint.theta = constrainAngle(phi+PI/2);
rsavitski 56:ed585a82092b 68
rsavitski 56:ed585a82092b 69 //arm offset
rsavitski 56:ed585a82092b 70 current_waypoint.x += 0.0425*cos(current_waypoint.theta);
rsavitski 56:ed585a82092b 71 current_waypoint.y += 0.0425*sin(current_waypoint.theta);
madcowswe 67:be3ea5450cc7 72
madcowswe 67:be3ea5450cc7 73 current_waypoint.pos_threshold = 0.01;
madcowswe 67:be3ea5450cc7 74 current_waypoint.angle_threshold = 0.01*PI;
rsavitski 57:d434ceab6892 75
rsavitski 69:4b7bb92916da 76 //motion::setNewWaypoint(Thread::gettid(),&current_waypoint);
rsavitski 69:4b7bb92916da 77 if (firstavoidstop)
rsavitski 69:4b7bb92916da 78 {
madcowswe 66:f1d75e51398d 79 motion::collavoiden = 0;
madcowswe 66:f1d75e51398d 80 firstavoidstop = 0;
madcowswe 66:f1d75e51398d 81 }
madcowswe 66:f1d75e51398d 82 else
madcowswe 66:f1d75e51398d 83 motion::collavoiden = 1;
rsavitski 30:791739422122 84 }
rsavitski 54:99d3158c9207 85 motion::waypoint_flag_mutex.unlock();
rsavitski 54:99d3158c9207 86
rsavitski 54:99d3158c9207 87 Thread::wait(50);
rsavitski 30:791739422122 88 }
rsavitski 30:791739422122 89 }
rsavitski 30:791739422122 90
rsavitski 70:0da6ca845762 91 void arm_upper(const void *dummy)
rsavitski 70:0da6ca845762 92 {
rsavitski 70:0da6ca845762 93 arm::upper_arm.go_up();
rsavitski 70:0da6ca845762 94 }
rsavitski 70:0da6ca845762 95
rsavitski 69:4b7bb92916da 96 void delayed_setter(const void *tid_wpptr) //TODO: kill the hack
rsavitski 69:4b7bb92916da 97 {
rsavitski 69:4b7bb92916da 98 delayed_struct *dsptr = (delayed_struct *)tid_wpptr;
rsavitski 70:0da6ca845762 99 //arm::upper_arm.go_up();
rsavitski 69:4b7bb92916da 100 motion::setNewWaypoint(dsptr->tid,dsptr->wpptr);
rsavitski 69:4b7bb92916da 101 delayed_done = true;
rsavitski 69:4b7bb92916da 102 }
rsavitski 69:4b7bb92916da 103
rsavitski 30:791739422122 104 } //namespace