Skip to content

Commit 929ba55

Browse files
committed
Add robot.c version 3.6
1 parent 5a8bc43 commit 929ba55

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed

robot.c

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/* robot3_6.c
2+
*
3+
* These messages are also used by the Java program robot3.Controller
4+
*
5+
* Messages, PC => RCX
6+
* * R = reset, motor powers to 0 and motors to float
7+
* S = stop (brake, full voltage to motors)
8+
* Mxaybzc = Set motor state and speed, x=speed for A, a=state for A,
9+
* y=speed for B, b=state for B...
10+
* Ax = set motor A's power to the value of x (binary, 0..255)
11+
* Bx = set motor B's power to the value of x (binary)
12+
* Cx = set motor C's power to the value of x (binary)
13+
* 1x = set motor A state to x, (0=off (float), 1=forward, 2=reverse, 3=brake)
14+
* defined in robot.Controller.[FLOAT|FORWARD|REVERSE|BRAKE] )
15+
* 2x = set motor B state to x
16+
* 3x = set motor C state to x
17+
*
18+
* -----------------------------------------------------------------------------
19+
*
20+
* by Motti Frimer 2003
21+
*/
22+
23+
#include <conio.h>
24+
#include <unistd.h>
25+
#include <string.h>
26+
#include <lnp.h>
27+
#include <dmotor.h>
28+
#include <dsensor.h>
29+
#include <time.h>
30+
#include <lnp-logical.h>
31+
32+
unsigned char sensor_data[18];
33+
34+
unsigned char motor_A_speed;
35+
unsigned char motor_A_dir;
36+
unsigned char motor_B_speed;
37+
unsigned char motor_B_dir;
38+
unsigned char motor_C_speed;
39+
unsigned char motor_C_dir;
40+
41+
unsigned char index;
42+
43+
#define Abs(x) ((x >= 0) ? x : -x)
44+
45+
int index_out_A;
46+
int index_out_B;
47+
48+
int rotation_sens_old_A;
49+
int rotation_sens_old_B;
50+
51+
unsigned int wait;
52+
int counter;
53+
54+
55+
/* Brakes with all the motors */
56+
void stop_robot() {
57+
motor_a_dir(brake);
58+
motor_b_dir(brake);
59+
motor_c_dir(brake);
60+
motor_a_speed(255);
61+
motor_b_speed(255);
62+
motor_c_speed(255);
63+
}
64+
65+
void my_integrity_handler(const unsigned char *data, unsigned char len)
66+
{
67+
if(data[0]=='S') {
68+
stop_robot();
69+
}
70+
else if(data[0]=='R') {
71+
motor_a_dir(off);
72+
motor_b_dir(off);
73+
motor_c_dir(off);
74+
motor_a_speed(0);
75+
motor_b_speed(0);
76+
motor_c_speed(0);
77+
}
78+
else if(data[0]=='M') {
79+
motor_a_dir(data[2]);
80+
motor_b_dir(data[4]);
81+
motor_c_dir(data[6]);
82+
motor_a_speed(data[1]);
83+
motor_b_speed(data[3]);
84+
motor_c_speed(data[5]);
85+
}
86+
else if(data[0]=='A') {
87+
motor_a_speed(data[1]);
88+
motor_A_speed = data[1];
89+
}
90+
else if(data[0]=='B') {
91+
motor_b_speed(data[1]);
92+
motor_B_speed = data[1];
93+
}
94+
else if(data[0]=='C') {
95+
motor_c_speed(data[1]);
96+
motor_C_speed = data[1];
97+
}
98+
else if(data[0]=='1') {
99+
motor_a_dir(data[1] );
100+
motor_A_dir = data[1];
101+
}
102+
else if(data[0]=='2') {
103+
motor_b_dir(data[1] );
104+
motor_B_dir = data[1];
105+
}
106+
else if(data[0]=='3') {
107+
motor_c_dir(data[1] );
108+
motor_C_dir = data[1];
109+
}
110+
else if(data[0]=='I') {
111+
index = data[1];
112+
}
113+
114+
}
115+
116+
117+
118+
int main()
119+
{
120+
motor_A_speed = 0;
121+
motor_A_dir = 0;
122+
motor_B_speed = 0;
123+
motor_B_dir = 0;
124+
motor_C_speed = 0;
125+
motor_C_dir = 0;
126+
127+
index = 0;
128+
index_out_A = 0;
129+
index_out_B = 0;
130+
131+
counter=0;
132+
133+
rotation_sens_old_A = 0;
134+
rotation_sens_old_B = 0;
135+
136+
wait = 500;
137+
138+
139+
ds_active(&SENSOR_1); /* activates sensor on port 1 */
140+
ds_rotation_on(&SENSOR_1); /* activates rotation sensor on port 1 */
141+
ds_rotation_set(1,10); /* sets initial value that sensor returns */
142+
143+
ds_active(&SENSOR_2); /* activates sensor on port 2 */
144+
ds_rotation_on(&SENSOR_2); /* activates rotation sensor on port 2 */
145+
ds_rotation_set(2,10); /* sets initial value that sensor returns */
146+
147+
148+
149+
lnp_integrity_set_handler(my_integrity_handler);
150+
lnp_logical_range(0); // sets the transmitter mode to the short range
151+
152+
while(1)
153+
{
154+
155+
156+
157+
rotation_sens_old_A = ROTATION_1;
158+
rotation_sens_old_B = ROTATION_2;
159+
160+
msleep(wait);
161+
162+
sensor_data[0]='s';
163+
sensor_data[1]= (unsigned char)(ROTATION_1 & 0xFF);
164+
sensor_data[2]= (unsigned char)((ROTATION_1 & 0xFF00) >> 8);
165+
sensor_data[3]= (unsigned char)(ROTATION_2 & 0xFF);
166+
sensor_data[4]= (unsigned char)((ROTATION_2 & 0xFF00) >> 8);
167+
sensor_data[5]= (unsigned char)(TOUCH_3 & 0xFF);
168+
sensor_data[6]= (unsigned char)((TOUCH_3 & 0xFF00) >> 8);
169+
170+
/* Motor data */
171+
sensor_data[7]= motor_A_speed;
172+
sensor_data[8]= motor_A_dir;
173+
sensor_data[9]= motor_B_speed;
174+
sensor_data[10]= motor_B_dir;
175+
sensor_data[11]= motor_C_speed;
176+
sensor_data[12]= motor_C_dir;
177+
178+
179+
/* Multiply by 2=(1/(wait/1000)) in order to compute the speed per sec */
180+
181+
index_out_B=Abs(( ROTATION_2 - rotation_sens_old_B )*2);
182+
index_out_A=Abs(( ROTATION_1 - rotation_sens_old_A )*2);
183+
sensor_data[13]= (unsigned char)(index_out_A & 0xFF);
184+
sensor_data[14]= (unsigned char)((index_out_A & 0xFF00) >> 8);
185+
sensor_data[15]= (unsigned char)(index_out_B & 0xFF);
186+
sensor_data[16]= (unsigned char)((index_out_B & 0xFF00) >> 8);
187+
188+
lnp_integrity_write(sensor_data, sizeof(sensor_data));
189+
190+
if(TOUCH_3!=0) //if sensor has been touched
191+
{
192+
motor_a_dir(rev);
193+
motor_b_dir(fwd);
194+
sleep(3);
195+
motor_a_dir(brake);
196+
motor_b_dir(brake);
197+
198+
}
199+
200+
201+
202+
}
203+
return 0;
204+
}

0 commit comments

Comments
 (0)