post
poster: Xorm
description: Housebot_002_functions
language: plain text
[download]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

#use "xbccamlib.ic"
#use defines.ic

void grab_blue_balls()
// does some specialized stuff that aids in the grabbing
// of the balls and then proceeds to GRAB THEM... ohnoes!
// It basically shifts a set amount to the left to compensate
// for the offcenter claw, drops the claw, jiggles back and
// forth a bit, and grabs it. I _think_ it'll work. ~xorm
// also if it fails in grabbing the oh-so-holy balls, it will;
// back way up and hit up 
{
    ao();
    // schloop left
    motor(PORT_MOTOR,-50);
    motor(STBD_MOTOR,50);
    sleep(0.75);
    bk(PORT_MOTOR); bk(STBD_MOTOR);
    
}

int check_ball_range()
// for the blue balls, checks their range. if they're in range
// for grabbing, it returns 1. May be expanded to have more
// deciding measurements, such as blob size. That's why it has
// its own function.
{
    
    if(get_et_dist() <= DIST_TO_OBJECT_FROM_ET) return 1;
    else return 0;
}

void get_blue_ball()
// tracks and moves towards the blue ball.
// when it gets to the ball (as detected by ET)
// it will halt (HAMMERZEIT) and drop the claw.
{
    
    int x = 0; //dummy variable so for() won't complain
    x=start_process(track()); // start tracking daemon
    while(1)
      { // corrects path based on track_d
        for(track_d==1;track_d==1;x=1) left();
        for(track_d==2;track_d==2;x=1) right();
        for(track_d==3;track_d==3;x=3) fwd();
        
        if( check_ball_range() == 1) break;
        
        // if the ball is found to be within range
        // break out of the tracking function and GO GET 'ER
    }
        printf("ATTEMPTING TO GRAB A BLUE BALL\n");
        grab_blue_balls(); // lolz.
}
    

int get_et_dist()
{
    return (255-analog(IR_SENS));
    //  makes the value correspond positively to distance
    // instead of the other way around. Why is it even like that? =\
}

void update_camdata()
{
    init_camera();
    while(1){
        if(track_is_new_data_available()==1)
          {
            track_update();
            cur_blob_pos = track_x(CHANNEL,INDEX);
            printf("cur_blob_pos = %d\n", cur_blob_pos); // diag data
            sleep(0.25);
        }
    }
}


void initialize_servo_positions()
{
    set_servo_position(CLAW_SERVO,CLAW_POS_UP);
}


void killswitch()
{       // kills the bot when either 89 seconds are up
    // or someone hits A and B at the same time.
    /* INSERT CODE: function call to botball routines */
    for(;;) {
        if(check_button(1) == 1 && check_button(2) == 1)
          {
            disable_servos();
            /* INSERT KILL FUNCTION [die()??] */
        }
    }
}

// #################### END IN-PROGRESS ################



// FUNCTIONS BEYOND  THIS POINT ARE CONSIDERED FINISHED.
// #####################################################

void raise_claw()
{
    set_servo_position(CLAW_SERVO, CLAW_POS_UP);
    sleep(CLAW_ACT_SLEEP);
}
void drop_claw()
{
    set_servo_position(CLAW_SERVO, CLAW_POS_DOWN);
    sleep(CLAW_ACT_SLEEP);
}

void claw_grab() // actually just grabs something with the claw.
{
    raise_claw(); // swings the ball claw up
    act_claw(0); //opens claw
    drop_claw(); // drops the claw. DUH!
    act_claw(1); // closes the claw
    raise_claw(); // picks up anything in the claw.
}

void act_claw(int dir)
{ // moves the claw based on the number passed to the function.
    // act_claw(0); opens the claw, act_claw(1); closes it.
    if(dir==1){ motor(CLAW_MOTOR, -100);
        sleep(CLAW_SLEEP);
        motor(CLAW_MOTOR, 0);
    }   
    if(dir==0){ motor(CLAW_MOTOR, 100);
        sleep(CLAW_SLEEP);
        motor(CLAW_MOTOR, 0);
    }
    sleep(0.5);
}

void left()
{
    printf("L");
    motor(STBD_MOTOR, MF);
    motor(PORT_MOTOR, (MF/2));
}

void right()
{
    printf("R");
    //    ao();
    motor(PORT_MOTOR, MF);
    motor(STBD_MOTOR, (MF/2));
}

void fwd()
{
    printf("F");
    ao();
    motor(STBD_MOTOR, FF);
    motor(PORT_MOTOR, FF);    
}

void bkwd()
{
    printf("F");
    ao();
    motor(STBD_MOTOR, (-1)*FF);
    motor(PORT_MOTOR, (-1)*FF);    
}

void track() // checks camera and returns 
// 1 for left, 2 for right, 3 for forwards
// path correction. stores value in track_d
{
    int x_pos = 0;
    init_camera();// bring up DA CAMERA
    while(1)
      {
        if(track_is_new_data_available()==1)
          {
            track_update();
            x_pos = track_x(CH,I);
            
            if(x_pos < CAM_CENTER - DZ)track_d = 1;            
            if(x_pos > CAM_CENTER + DZ)track_d = 2;          
            if(x_pos<CAM_CENTER+DZ&&x_pos>CAM_CENTER-DZ)track_d = 3;  
        }
    }
    
}

void drop_umbrella() //self explanatory
{
    motor(UMBRELLA_MOTOR, 100); //Intended to snap down, to prevent umbrellas from
    sleep(1.0);                    // turning, but if this doesn't work, gear ratios
    off(UMBRELLA_MOTOR);
}