Project

General

Profile

Revision 321

Recharging still doesn't work with analog - analog not giving correct values.

View differences:

branches/autonomous_recharging/code/projects/autonomous_recharging/dragonfly/main.c
12 12
int main(void)
13 13
{
14 14
	dragonfly_init(ALL_ON);
15
	usb_puts("Initializing wireless.\n");
16
	range_init();
17
	orb_enable();
15
	//range_init();
16
	//orb_enable();
17
	usb_puts("Turned on!\n");
18 18
	wl_init();
19
	usb_puts("Wireless initialized!\n");
19 20
	wl_set_channel(0xA);
20 21
	//orb_set_color(YELLOW);
21 22
	wl_token_ring_register();
22
	wl_token_ring_set_bom_functions(do_nothing2, do_nothing2, get_nothing2); 
23
	//wl_token_ring_set_bom_functions(do_nothing2, do_nothing2, get_nothing2); 
23 24
	wl_token_ring_join();
24
	usb_puts("Wireless initialized.\n");
25
	recharge_init();
26
	usb_puts("Recharging initialized.\n");
27
	//run_around_init();
25
	//usb_puts("Wireless initialized.\n");
26
	//recharge_init();
27
	//usb_puts("Recharging initialized.\n");
28
	run_around_init();
28 29
	while (1)
29 30
	{
30 31
		wl_do();
31
		int charging = recharge_do();
32
		if (!charging)
32
		int i;
33
		for (i = 6; i > 0; i--)
33 34
		{
34
			analog8(IR5);
35
			usb_puti(analog_get8(i));
36
			usb_putc(' ');
37
		}
38
		usb_putc('\n');
39
		//int charging = recharge_do();
40
		//if (!charging)
41
		//{
42
			//analog8(IR1);
35 43
			//run_around_FSM();
36
		}
44
		//}
37 45
	}
38 46
	
39 47
	return 0;
branches/autonomous_recharging/code/projects/autonomous_recharging/dragonfly/smart_run_around_fsm.c
1
#include "dragonfly_lib.h"
2
#include "smart_run_around_fsm.h"
3

  
4
/*A simple behavior for navigating in an environment, i.e. avoiding walls and getting stuck.
5
Could be better at not getting stuck.
6

  
7
Latest revision only has two accessible states: move and reverse.
8
*/
9

  
10

  
11
void run_around_init(void)
12
{
13
//  range_init();
14
//  analog_init();
15
//  motors_init();
16
//  orb_init();
17
//  orb_enable();
18
//  usb_init();
19
 
20
  /*Start in the default state, MOVING*/ 
21
  avoid_state=MOVING;
22
  /*Set timers to their maximum values.*/
23
  crazy_count=CRAZY_MAX;
24
  backup_count=0; 
25
  pControl=0;
26
  
27
  /*Initialize distances to zero.*/ 
28
  d1=0; d2=0; d3=0; d4=0; d5=0;
29
  
30
  orb_set_color(GREEN);
31

  
32
}
33

  
34
/*The main function, call this to update states as frequently as possible.*/
35
void run_around_FSM(void) {
36
  /*Default to moving.*/ 
37
  avoid_state=MOVING;
38
  
39
  /*The following lines ensure that undefined (-1) values
40
  will not update the distances.*/ 
41
  int temp;
42
  
43
  temp=range_read_distance(IR1);
44
  d1=(temp == -1) ? d1 : temp;
45
  
46
  temp=range_read_distance(IR2);
47
  d2=(temp == -1) ? d2 : temp;
48
  
49
  temp=range_read_distance(IR3);
50
  d3=(temp == -1) ? d3 : temp;
51
  
52
  temp=range_read_distance(IR4);
53
  d4=(temp == -1) ? d4 : temp;
54
  
55
  temp=range_read_distance(IR5);
56
  d5=(temp == -1) ? d5 : temp;
57
  
58
  usb_puti(d1);
59
  usb_putc(' ');
60
  usb_puti(d2);
61
  usb_putc(' ');
62
  usb_puti(d3);
63
  usb_putc('\n');
64
  
65
  //If the crazy count is in it's >>3 range, it acts crazy.
66
  if(crazy_count<=(CRAZY_MAX>>3))
67
  {
68
    avoid_state=CRAZY;
69
    crazy_count--;
70
    
71
    if(crazy_count<0) crazy_count=CRAZY_MAX;
72
    
73
    evaluate_state();
74
    return;
75
  }
76
  
77
  //Checks the forward distance to see if it should back up, if so...state backwards.
78
  if((d2!=-1)&&(d2 < 150)){
79
      backup_count=BACKUP_MAX;
80
      avoid_state=BACKWARDS;
81
      evaluate_state();
82
      return;
83
  }
84
  if(backup_count<BACKUP_MAX){
85
    avoid_state=BACKWARDS; 
86
    if(backup_count<0)
87
      backup_count=BACKUP_MAX;
88
    evaluate_state();
89
    return;
90
  }
91
  
92
  //Should evaluate an expression from -255 to 255 to pass to move.
93
  pControl= ((d3-d1) + (d4-d5)) >> TURN_CONSTANT;
94
  
95
  if(pControl>PCONTROL_CRAZY_LIMIT || pControl<-PCONTROL_CRAZY_LIMIT) crazy_count--;
96
  //i.e. if you really want to turn for an extended period of time...you're probably stuck.
97

  
98
  /*Debug stuff:*/
99
  /*usb_puts("pControl evaluating: ");
100
  usb_puti(pControl);
101
  usb_puts("\n\r");
102
  usb_puts("IR1: ");
103
  usb_puti(d1);
104
  usb_puts(" IR2: ");
105
  usb_puti(d2);
106
  usb_puts(" IR3: ");
107
  usb_puti(d3);
108
  usb_puts(" IR4: ");
109
  usb_puti(d4);
110
  usb_puts(" IR5: ");
111
  usb_puti(d5);
112
  usb_puts("\n\r");*/
113
  
114
  evaluate_state();
115
}
116

  
117

  
118
//Acts on state change.
119
void evaluate_state(){
120
    switch(avoid_state){
121
    case(MOVING): orb_set_color(GREEN);
122
      move(STRAIT_SPEED,-pControl);
123
      break;
124
    
125
    case(BACKWARDS): orb_set_color(ORANGE);
126
      move(-STRAIT_SPEED,0);
127
      break;
128
      
129
    case(CRAZY): orb_set_color(RED);
130
      /*TODO: Implement a crazy state.*/
131
      move(STRAIT_SPEED,-pControl);
132
      break;
133
      
134
    default:
135
      /*Should never get here, go strait.*/
136
      move(100,0); orb_set_color(BLUE);
137
      break;
138
  }
139
}
140

  
141

  
branches/autonomous_recharging/code/projects/autonomous_recharging/dragonfly/Makefile
15 15
 USE_WIRELESS = 1
16 16

  
17 17
# com1 = serial port. Use lpt1 to connect to parallel port.
18
AVRDUDE_PORT = /dev/ttyUSB1
18
AVRDUDE_PORT = /dev/ttyUSB0
19 19
#
20 20
#
21 21
###################################
branches/autonomous_recharging/code/projects/autonomous_recharging/dragonfly/smart_run_around_fsm.h
1
//Obstacle Avoid Numbers
2

  
3

  
4
#ifndef _RUN_AROUND_FSM_H_
5
#define _RUN_AROUND_FSM_H_
6

  
7
//The States: 
8
#define MOVING 12           //Move strait.
9
#define BACKWARDS 15        //Move backwards. (Front close to wall.)
10
#define STOP 16             //Stop.  The default state, (Something broke).
11
#define CRAZY 40            //Erratic behavior that occurs more often when the robot is frequently trying to turn. (i.e. may be stuck.)
12

  
13
#define LEFT 37             //Left
14
#define RIGHT 39            //Right
15

  
16
#define BACKUP_MAX 20
17
#define CRAZY_MAX 200       //The number of counts between "crazy moments"
18
#define STRAIT_SPEED 185    //The speed when going strait or backing up.
19
#define TURN_CONSTANT 2
20
#define PCONTROL_CRAZY_LIMIT 80
21

  
22
int avoid_state;    /*State machine variable.*/
23
int crazy_count;    /*Counter for a 'get unstuck' behavior.*/
24

  
25
int backup_count;	/*Counter for backup duration.*/
26
int pControl;		/*Proportional control variable, determines turn direction.*/
27
int d1,d2,d3,d4,d5;	/*The five distances taken in by IR.*/
28

  
29
void run_around_init(void);
30
void run_around_FSM(void);
31
void evaluate_state(void);
32

  
33
#endif

Also available in: Unified diff