You are on page 1of 55

()

0110200315

(
):

2005 6

PC RS-485

RS485
NQC ROBOLAB
RCX

RS-485

RCX

Title

Design of The Traffic Management Simulating System

Design of the Software

Abstract
This design is The Traffic Management Simulating System. A SCM controls
the regulation of the traffic system and the traffic lights. The vehicles
move according to the infrared signal transferred by traffic panels. In
order to control the whole system, we build a communication system by
connecting SCM with PC. In this paper, the software design of this system
consists essentially of two parts, the embedded system software design and
the vehicle software design. In the first part, the Task_Schedule will be
used as the embedded operating system. And according to the function of
the system, the system can be divided into three modules, including the
display module, the infrared communication module and the RS-485
communication module. Above all, the Task_Schedule determines which module
will be carried out. In the second part, the vehicles are programmed by
ROBOLAB and NQC. After all, the emphasis is put on the design of the modules
and vehicles. At last, the principles of Task_Schedule and RCX are
illustrated. All the programs have been testified in the system.

Keywords Task_schedule

Display of the traffic lights

Infrared Communication

RS-485 Communication

RCX

I II

1 ................................................................ 1
1.1 ............................................................... 1
1.2 ......................................... 1
2 .............................................................. 3
2.1 ....................................................... 3
2.2 ......................................... 3
2.3 ....................................................... 4
3 .................................................. 5
3.1 ............................................... 5
3.2 ....................................................... 7
3.3 ........................................................... 8
3.4 .......................................................... 11
4 ................................................... 12
4.1
4.2
4.3
4.4

..................................................
..................................................
............................................
..........................................................

12
13
14
15

4.5 ..........................................................
5 RS-485 .................................................
5.1 RS-485 ..............................................
5.2 RS-485 ..............................................
5.3 ..........................................................
5.4 ..........................................................
6 .....................................................

15
17
17
17
18
18
20

6.1 RCX ........................................................... 20


6.2 ROBOLAB ................................................... 21
6.3
6.4
6.5

NQC .......................................................
................................................
..........................................................
................................................................
................................................................

22
23
24
29
30

........................................................... 31


A
B
C

II II

......................................... 33
NQC ...................................................... 47
ROBOLAB .................................................. 50

1 50

1
1.1

4.4m4.4m

PC

1.2

RS-485

RS-485

PC
Embedded
Real Time Operating SystemERTOS
1.1

2 50

PC
(
)

RS-485

1.1

NQC(Not quite C) ROBOLAB

1
2
3
4
5
RS-485
6 the Robotic Command Explorer
RCX ROBOLAB NQC ROBOLAB
NQC A 3 6
B NQC C ROBOLAB

3 50

RS-485

2.1

2.2

CPU

4 50

1 X 2 X
1
X

2.3

runme 1
runme 1

runme 1 runme 1

5 50

struct

(code *task_p)(void);
delay;
period;
runme;
co_op;

void
unsigned char
unsigned char
unsigned char
bit

};

task_p delay period


co_op 1 0
delay 0 peroid 0

37
374148 32 I/O

3.1

3.1 34
LED

LED

6 50

( )

3.1

3.1
8 3.2
SERIAL_DATA_INPUT
SHIFT_CLOCK LATCH_CLOCK
SHIFT_CLOCK
SERIAL_DATA_INPUT
LATCH_CLOCK

Q0

Q6

Q0

STCP

SHCP

SHCP

DS

SHIFT_CLOCK

Q6

Q0

DS

STCP

SHCP

Q6

Q0

STCP

SHCP

LATCH_CLOCK1

SERIAL_DATA_INPUT

LATCH_CLOCK2

DS

LATCH_CLOCK3

3.2

Q5 Q6

4
DS

STCP

LATCH_CLOCK4

Q0

Q6

SHCP

DS

STCP

LATCH_CLOCK5

7 50

3.2

3.1

3.1

3.2

3.2

8s

2s

8s

30s

2s

8s

2s

8s

2s

8s

30s

2s

8s

2s

8 50

3.3

148

74

3.2 5 4
7
7
0 1 9 11
11
0x00
7

9 1
7 4 3.2

0x00 0x01
&

A 3.3

9 50

10

1s 1
2 0

12

34

12

34

10 50

1
N

=2
Y

=2
Y

11 50

=0
Y

=0
Y

3.3
3.4
LATCH_CLOCK

12 50

4.1

4.1

4.1

13 50

4.2

4.2

25
/

40kHz)
LED

LED
5V
IR

0V
4.2

4.2 LED

14 50

IR
32kHz 45kHz

4.3

RCX

0x55 0xff 0x00

0xf7

0x08

D ~D C

~C

D , C = f7D~D ~C D C
0x55

0xff

0x00

0xf7

0x08

0x12

0xed

0x09

0xf6

0x12

38kHz

2400b/s

15 50

4.1

4.1

0x01

0x55 0xff 0x00 0xf7 0x08 0x01 0xfe 0xf8 0x07

0x02

0x55 0xff 0x00 0xf7 0x08 0x02 0xfd 0xf9 0x06

0x03

0x55 0xff 0x00 0xf7 0x08 0x03 0xfc 0xfa 0x05

0x04

0x55 0xff 0x00 0xf7 0x08 0x04 0xfb 0xfb 0x04

4.4
38KHz

4 1 2 3 4
typedef tByte unsigned char;
tByte code
tByte code
tByte code
tByte code

message1_table[ ]={0x55,0xff,0x00,0xf7,0x08,0x01,0xfe,0xf8,0x07};
message2_table[ ]={0x55,0xff,0x00,0xf7,0x08,0x02,0xfd,0xf9,0x06};
message3_table[ ]={0x55,0xff,0x00,0xf7,0x08,0x03,0xfc,0xfa,0x05};
message4_table[ ]={0x55,0xff,0x00,0xf7,0x08,0x04,0xfb,0xfb,0x04};

A 4.3

11 2400b/s

4.5

0xff 0xff

16 50

1
1m
30
38kHz 2400b/s
2400b/s0
14


message1[]


message2[]


message3[]


message4[]


message1[]


message2[]


message3[]

4.3


message4[]

17 50

5 RSRS-485
PC 12
12 RS-485

5.1 RSRS-485
RS-485

8051 2 3 RS485
SM2 1
8 9 /
1
SM2
SM2=1,

5.2 RSRS-485
1 PC 1 RS-232/485 12
RS-232/485 PC
RS-232/485 5.1
1 8 1 1
2400
01H12H

18 50



ffH

f1H

f2H

f3H

f4H

RS-232

RS232/485

5.1

RS-485

RS-485

.
...

12

5.3
RS485 A
5.2

SM2 0

0xff

5.4
RS485 RS-485

RS-485 RS-485

RS-485

19 50

SM2=0, RB8=0

SM2=0

5.2

RS-485

20 50

ROBOLAB NQC

6.1 RCX
the Robotic Command ExplorerRCX
RCX 3 3
RCX 5 RCX event-oriented
10

6.1

RCX

RCX


21 50

DC

RCX RCX
RCX

RCX 10

A/D 10 A/D

2000 RCX
ROBOLAB

Multi-agency

RCX RCX

RCX RCX

6.2 ROBOLAB
ROBOLAB (NI) LabVIEW
ROBOLAB ROBOLAB

A.
( 6.2 )

6.2
B.

22 50

6.3

6.3
C.

6.4

6.4
6.3 NQC
NQC Not Quite C C Dave Baum
RCX C
RCX NQC ROBOLAB
NQC ROBOLAB NQC
NQC
Windows/Linux/Mac NQC
ROBOLAB

23 50

Itegrated Development Environment-----IDE


DOS NQC IDE IDE
Bricx Command CenterBricxCC NQC
RCX
BricxCC 6.5

6.5

BicxCC

6.4
RCX 6.6
RCX 3

24 50

6.7

6.6

6.7

6.5
ROBOLAB NQC

NQC ROBOLAB

1s
B 6.8

25 50

B
6.9

6.8

6.9

B 6.10

26 50

6.7
B 6.11

?
Y

2s

6.10

6.11

27 50

6.12

Sem=?

SEM+=1

6.12

28 50

B 6.12

29 50

RS-485

ROBOLAB NQC

RCX RCX

30 50

1007

31 50

1 . C . 3 .
2003
2 . 8051 . 1 .
2003
3 .8051 .2004
4 .MCS-51 C .
2003
5 Michael J.Pont.C .2003
6
7

.PC .2004
Michael J.Pont..

Fred G. Martin..2004

2004

9 ROBOLAB 2.5 (ver0.9.9).www.semia.com2003


10 ROBOLAB (ver1.0.1).www.semia.com2003
11 ActiveX RCX.www.ceeo.com/activeX2004
12
Andrew OGrady.ROBOLAB Troubleshooting Guide.Tufts University
Press2003
13 ROBOLAB_Reference_Guide(version1.2).www.lego.com/education2002
14 Eric.L.WangChrisRogers.Engineering with LEGO bricks and ROBOLAB.
Department of Mechanical Engineering2002
15 Matthew Bates.NQC Programmer Reference.The Open University2000
16 Dave Baum.NQC Programmier Guide.www.robotdiy.com2003
17 William J.Taffe.Programming LEGO Robots Using NQC.Department of
Computer Science Utrecht University2002
18 Jonathan B.Knudsen.LEGO MINDSTORMS Robots.www.oreilly.com/catalog
1999
19 Walton Hall Milton Keynes.What Is the Best Environment Language
For Teaching Robotics Using LEGO MindStorms.The Open University2002
20 David HanleySean Hearne.LEGO Mindstorms programming with VB.WIT
Press2002
21 Quickstart Guide.www.LEGO.com/education/MINDSTORMS2004
22 Controlling LEGO Programmable Bricks Technical Reference.www.ceeo.
com/ Pbrick1998

32 50

23 Fred G. Martin.The Art of LEGO Design.The Robotics Practitioner


Press1995
24 .51 C51 .
2002
25 ..2003
26 .MCS-51 C .
2003

1 port.h
#ifndef _port_h
#define _port_h
sbit SERIAL_DATA_INPUT_NORTH=P1^0;
sbit SERIAL_DATA_INPUT_SOUTH=P1^1;
sbit SERIAL_DATA_INPUT_WEST=P1^2;
sbit SERIAL_DATA_INPUT_EAST=P1^3;
sbit SHIFT_CLOCK_NORTH=P1^4;
sbit SHIFT_CLOCK_SOUTH=P1^5;
sbit SHIFT_CLOCK_WEST=P1^6;
sbit SHIFT_CLOCK_EAST=P1^7;
sbit LATCH_CLOCK1=P2^0;
sbit LATCH_CLOCK2=P2^1;
sbit LATCH_CLOCK3=P2^2;
sbit LATCH_CLOCK4=P2^3;
sbit LATCH_CLOCK5=P2^4;
sbit DE=P3^4;
sbit A0=P0^4;
sbit A1=P0^5;
sbit A2=P0^6;
sbit A3=P0^7;
sbit SW1=P0^0;
sbit SW2=P0^1;
sbit SW3=P0^2;
sbit SW4=P0^3;
#endif

2 main.h
#ifndef _main_h
#define _main_h

33 50



#include <reg52.h>
#define osc_freq (12000000UL)
#define osc_per_inst (12)
typedef unsigned char tByte;
typedef unsigned int tWord;
typedef unsigned long tLong;
#define interrupt_timer_0_overflow 1
#define interrupt_timer_1_overflow 3
#define interrupt_timer_2_overflow 5
#endif

3 main.c
#include "main.h"
#include "port.h"
#include "display.h"
#include "485.h"
#include "ir.h"
#include "2_01_12h.h"
tByte time_in_state_ns;
tByte time_in_state_we;
void main(void)
{
hsch_init_t2( );
initial( );
traffic_state_init( );
clear_screen( );
power_on( );
hsch_add_task(display,0,0,0);
hsch_add_task(slave_rev,0,0,0);
hsch_add_task(ir,0,0,0);
hsch_start( );
while(1)
{
hsch_dispatch_tasks( );
}

34 50



}
void initial(void)
{
SW1=1;
SW2=1;
SW3=1;
SW4=1;
slave=(P0&0x0f);
DE=0;
A2=0;
A3=0;
TMOD=0x21;
TH0=19450/256;
TL0=19450/256;
ET0=1;
EA=1;
SCON=0xD0; //SM0=SM1=1;SM2=0;REN=1;
TH1=0xf4;
TL1=0xf4;
TR0=1;
TR1=1;
}
void traffic_state_init(void)
{
traffic_state_we=FORWARD;
time_in_state_we=FORWARD_DURATION;
traffic_state_ns=STOP;
time_in_state_ns=STOP_DURATION;
}

4 2_01_12h.h
#include "main.h"
#include "hsch.h"
void hsch_init_t2(void);
void hsch_start(void);

5 2_01_12h.c
#include "2_01_12h.h"

35 50



extern staskh hsch_tasks_g[hsch_max_tasks];
void hsch_init_t2(void)
{
T2CON=0x04;
T2MOD=0x00;
TH2=0xfc;
PCAP2H=0xfc;
TL2=0x18;
PCAP2L=0x18;
ET2=1;
TR2=1;
}
void hsch_start(void)
{
EA=1;
}
void hsch_update(void) interrupt interrupt_timer_2_overflow
{
tByte index;
TF2=0;
for(index=1;index<hsch_max_tasks;index++)
{
if(hsch_tasks_g[index].ptask)
{
if(hsch_tasks_g[index].delay==0)
{
if(hsch_tasks_g[index].co_op)
{
hsch_tasks_g[index].runme+=1;
}
else
{
(*hsch_tasks_g[index].ptask)( );
hsch_tasks_g[index].runme-=1;
if(hsch_tasks_g[index].period==0)
{
hsch_tasks_g[index].ptask=0;
}
}
else
{

36 50

37 50

hsch_tasks_g[index].delay=hsch_tasks_g[index].period;
}
}
}
}
}

6 hsch.h
#ifndef _hsch_h
#define _hsch_h
#define main.h
typedef struct
{
void (code *task_p)(void);
tWord delay;
tWord period;
tWord runme;
bit co_op;
}staskh;
void sch_dispatch_tasks(void);
void hsch_add_task(void (code *)(void),tWord,tWord,bit);
#define hsch_max_tasks (3)
#endif

7 hsch.c
#include "main.h"
#include "port.h"
#include "hsch.h"
staskh hsch_tasks_g[hsch_max_tasks];
static void hsch_go_to_sleep(void);
void hsch_dispatch_tasks(void)
{
tByte index;
for(index=0;index<hsch_max_tasks;index++)
{
if((hsch_tasks_g[index].co_op)&&(hsch_tasks_g[index].runme)
{

38 50

(*hsch_tasks_g[index].ptask)( );
hsch_tasks_g[index].runme-=1;
if(hsch_tasks_g[index].period==0)
{
hsch_tasks_g[index].ptask=0;
}
}
}
hsch_go_to_sleep( );
}
void hsch_add_task(void (code *fn_p)( ),tWord delay,tWord period,bit co_op)
{
tByte index=0;
while((hsch_tasks_g[index].ptask!=0)&&(index<hsch_max_tasks))
{
index++;
}
hsch_tasks_g[index].ptask=fn_p;
hsch_tasks_g[index].delay=delay;
hsch_tasks_g[index].period=period;
hsch_tasks_g[index].co_op=co_op;
hsch_tasks_g[index].runme=0;
}
void hsch_go_to_sleep(void)
{
PCON|=0x01;
}

8 display.h
#ifndef _display_h
#define _display_h
tByte code table[ ]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0x00};
//
0
1
2
3
4
5
6
7
8
9
tByte code traffic_signal_table[ ]={0x0c,0x14,0x64,0xa4,0x21,0x22,0x24};
tByte code compensation_table[ ]={0x01,0x01,0x00,0x00,0x01,0x01,0x01};
enum direction {north_south,west_east};
enum traffic_state {FORWARD,AMBER_F,LEFT,AMBER_L,RIGHT,AMBER_R,STOP}
traffic_state traffic_state_ns,traffic_state_we;
enum color {GREEN,RED} COL;

void clear_screen(void);



void led_update(tByte flag,tByte signal_ns,tByte signal_we);
void display(void);
#endif

9 display.c
#include "main.h"
#include "port.h"
#include "display.h"
tByte FORWARD_DURATION=10;
tByte LEFT_DURATION=10;
tByte RIGHT_DURATION=10;
tByte STOP_DURATION=10;
tByte flag;
tByte signal;
void clear_screen(void)
{
tByte i;
LATCH_CLOCK1=0;
LATCH_CLOCK2=0;
LATCH_CLOCK3=0;
LATCH_CLOCK4=0;
LATCH_CLOCK5=0;
for(i=0;i<8;i++)
{
SHIFT_CLOCK_NORTH=0;
SHIFT_CLOCK_SOUTH=0;
SHIFT_CLOCK_WEST=0;
SHIFT_CLOCK_EAST=0;
SERIAL_DATA_INPUT_NORTH=0;
SERIAL_DATA_INPUT_SOUTH=0;
SERIAL_DATA_INPUT_WEST=0;
SERIAL_DATA_INPUT_EAST=0;
SHIFT_CLOCK_NORTH=1;
SHIFT_CLOCK_SOUTH=1;
SHIFT_CLOCK_WEST=1;
SHIFT_CLOCK_EAST=1;
}
LATCH_CLOCK1=1;
LATCH_CLOCK2=1;
LATCH_CLOCK3=1;

39 50



LATCH_CLOCK4=1;
LATCH_CLOCK5=1;
}
void led_update(tByte flag,tByte signal_ns,tByte signal_we)
{
tByte i;
switch(flag)
{
case 1: LATCH_CLOCK1=0;break;
case 2: LATCH_CLOCK2=0;break;
case 3: LATCH_CLOCK3=0;break;
case 4: LATCH_CLOCK4=0;break;
case 5: LATCH_CLOCK5=0;break;
}
for(i=0;i<8;i++)
{
SHIFT_CLOCK_NORTH=0;
SHIFT_CLOCK_SOUTH=0;
SHIFT_CLOCK_WEST=0;
SHIFT_CLOCK_EAST=0;
SERIAL_DATA_INPUT_NORTH=(signal_ns&(1<<i))>>i;
SERIAL_DATA_INPUT_SOUTH=(signal_ns&(1<<i))>>i;
SERIAL_DATA_INPUT_WEST=(signal_we&(1<<i))>>i;
SERIAL_DATA_INPUT_EAST=(signal_we&(1<<i))>>i;
SHIFT_CLOCK_NORTH=1;
SHIFT_CLOCK_SOUTH=1;
SHIFT_CLOCK_WEST=1;
SHIFT_CLOCK_EAST=1;
}
switch(flag)
{
case 1: LATCH_CLOCK1=1;break;
case 2: LATCH_CLOCK2=1;break;
case 3: LATCH_CLOCK3=1;break;
case 4: LATCH_CLOCK4=1;break;
case 5: LATCH_CLOCK5=1;break;
}
}
void display(void)
{
tByte number_high_class_ns,number_low_class_ns;
tByte number_high_class_we,number_low_class_we;

40 50



tByte num_high_ns,num_low_ns;
tByte num_high_we,num_low_we;
tByte green_left_ns,green_left_we;
tByte green_right_ns,green_right_we;
tByte red_left_ns,red_left_we;
tByte red_right_ns,red_right_we;
num_high_ns=time_in_state_ns/10;
num_low_ns=time_in_state_ns%10;
num_high_we=time_in_state_we/10;
num_low_we=time_in_state_we%10;
green_left_ns=10;
green_left_we=10;
green_right_ns=10;
green_right_we=10;
red_left_ns=10;
red_left_we=10;
red_right_ns=10;
red_right_we=10;
if(traffic_state_ns ==FORWARD||traffic_state_ns==AMBER_F)
{
green_left_ns=num_high_ns;
green_right_ns=num_low_ns;
}
else
{
red_left_ns=num_high_ns;
red_right_ns=num_low_ns;
}
if(traffic_state_we ==FORWARD||traffic_state_we==AMBER_F)
{
green_left_we=num_high_we;
green_right_we=num_low_we;
}
else
{
red_left_we=num_high_we;
red_right_we=num_low_we;
}
led_update(1,table[green_left_ns],table[green_left_we]);
led_update(2,table[green_right_ns],table[green_right_we]);

41 50

42 50

led_update(3,table[red_left_ns],table[red_left_we]);
led_update(4,table[red_right_ns]|compensation_table[traffic_state_ns]
,table[red_right_we]|compensation_table[traffic_state_we]);
led_update(5,traffic_signal_table[traffic_state_ns],traffic_signal_table[traffic_state_we]);
time_in_state_ns-=1;
time_in_state_we-=1;
if(time_in_state_ns==2)
{
switch(traffic_state_ns){
case FORWARD: traffic_state_ns=AMBER_F;break;
case LEFT:
traffic_state_ns=AMBER_L;break;
case RIGHT:
traffic_state_ns=AMBER_R;break;
default:break;
}
}
if(time_in_state_we==2)
{
switch(traffic_state_we){
case FORWARD: traffic_state_we=AMBER_F;break;
case LEFT:
traffic_state_we=AMBER_L;break;
case RIGHT:
traffic_state_we=AMBER_R;break;
default:break;
}
}
if(time_in_state_ns==0)
{
switch(traffic_state_ns)
{
case AMBER_F:
traffic_state_ns=LEFT;
time_in_state_ns=LEFT_DURATION;
break;
case AMBER_L:
traffic_state_ns=RIGHT;
time_in_state_ns=RIGHT_DURATION;
break;
case AMBER_
traffic_state_ns=STOP;
time_in_state_ns=STOP_DURATION;
break;
case STOP:
traffic_state_ns=FORWARD;
time_in_state_ns=FORWARD_DURATION;



break;
}
}
if(time_in_state_we==0)
{
switch(traffic_state_we)
{
case AMBER_F:
traffic_state_we=LEFT;
time_in_state_we=LEFT_DURATION;
break;
case AMBER_L:
traffic_state_we=RIGHT;
time_in_state_we=RIGHT_DURATION;
break;
case AMBER_R:
traffic_state_we=STOP;
time_in_state_we=STOP_DURATION;
break;
case STOP:
traffic_state_we=FORWARD;
time_in_state_we=FORWARD_DURATION;
break;
}
}
}

10

RS-485

485.h

#ifndef _485_h
#define _485_h
#define change_FORWARD_time 0xf1
#define change_LEFT_time 0xf2
#define change_RIGHT_time 0xf3
#define change_STOP_time 0xf4
#define power 0xee
#define reset 0xff
void power_on(void);
void error(void);
void transfer_receive(tByte d);
void slave_rev(void);
#endif

43 50

11

RS-485

485.c

#include "main.h"
#include "port.h"
#include "485.h"
tByte slave;
void power_on(void)
{
while(!RI);
RI=0;
}
void error(void)
{
DE=1;
SBUF=0xff;
while(!TI);
TI=0;
DE=0;
}
void transfer_receive(tByte d)
{
DE=1;
SBUF=d;
while(!TI);
TI=0;
DE=0;
while(!RI);
RI=0;
}
void slave_rev(void) //interrupt 4 using 2
{
RI=0;
if(SBUF==slave)
{
SM2=0;
transfer_receive(slave);
switch(SBUF)
{

44 50

45 50

case change_FORWARD_time:
transfer_receive(change_FORWARD_time);
FORWARD_DURATION=SBUF;
break;
case change_LEFT_time:
transfer_receive(change_LEFT_time);
LEFT_DURATION=SBUF;
break;
case change_RIGHT_time:
transfer_receive(change_RIGHT_time);
RIGHT_DURATION=SBUF;
break;
case change_STOP_time:
transfer_receive(change_STOP_time);
STOP_DURATION=SBUF;
break;
default:error( );
}
SM2=1;
}
}

12 ir.h
#ifndef _ir_h
#define _ir_h
tByte code message1_table[ ]={0x55,0xff,0x00,0xf7,0x08,0x01,0xfe,0xf8,0x07};
tByte code message2_table[ ]={0x55,0xff,0x00,0xf7,0x08,0x02,0xfd,0xf9,0x06};
tByte code message3_table[ ]={0x55,0xff,0x00,0xf7,0x08,0x03,0xfc,0xfa,0x05};
tByte code message4_table[ ]={0x55,0xff,0x00,0xf7,0x08,0x04,0xfb,0xfb,0x04};
void send_message(bit a1,bit a0,tByte message);
void ir(void);
#endif

13 ir.c
#include "main.h"
#include "port.h"
#include "ir.h"
void send_message(bit a1,bit a0,tByte message)
{
tByte i;

46 50

A1=a1;
A0=a0;
for(i=0;i<9;i++)
{
switch(message)
{
case 1:ACC=message1_table[i];break;
case 2:ACC=message2_table[i];break;
case 3:ACC=message3_table[i];break;
case 4:ACC=message4_table[i];break;
}
TB8=!P;
SBUF=ACC;
while(!TI);
TI=0;
}
}
void ir(void)
{
switch(traffic_state_ns)
{
case FORWARD: send_message(0,0,1);send_message(0,1,1);break;
case LEFT: send_message(0,0,2);send_message(0,1,2);break;
case RIGHT: send_message(0,0,3);send_message(0,1,3);break;
case STOP: send_message(0,0,4);send_message(0,1,4);break;
}
switch(traffic_state_we)
{
case FORWARD: send_message(1,0,1);send_message(1,1,1);break;
case LEFT: send_message(1,0,2);send_message(1,1,2);break;
case RIGHT: send_message(1,0,3);send_message(1,1,3);break;
case STOP: send_message(1,0,4);send_message(1,1,4);break;
}
}

47 50

B NQC

#define BLACK 45
#define WHITE 49
#define THRESHOLD (BLACK+WHITE)/2
#define left( ) On(OUT_A+OUT_C);Wait(63);Toggle(OUT_A);Wait(128);
#define right( ) On(OUT_A+OUT_C);Wait(90);Toggle(OUT_C);Wait(120);
int sem;
//

task check_turn_signal( )
{
while(true)
{
if(SENSOR_1<THRESHOLD-1)
{
PlaySound(SOUND_CLICK);
start turn;
}
}
}

//

task check_obstacle_signal( )

//

void init( )
{
SetSensorType(SENSOR_3,SENSOR_TYPE_LIGHT);
SetSensorMode(SENSOR_3,SENSOR_MODE_RAW);
SetSensor(SENSOR_2,SENSOR_LIGHT);
SetSensor(SENSOR_1,SENSOR_LIGHT);
ClearSensor(SENSOR_3);
ClearSensor(SENSOR_2);
ClearSensor(SENSOR_1);
ClearMessage( );
sem=0;
}

48 50

{
while(true)
{
if(SENSOR_3<500)
{
PlaySound(SOUND_DOUBLE_BEEP);
stop trace;
stop check_turn_signal;
Off(OUT_A+OUT_C);
until(SENSOR_3>530);
Wait(200);
start check_turn_signal;
start trace;
}
}
}
task turn( )
{
stop check_turn_signal;
stop trace;
sem+=1;
switch(sem)
{
case 1: break;
case 2:
{
if(Message( )= =2) left( );
else
{
Off(OUT_A+OUT_C);
until(Message( )==2);
left( );
}
break;
}
case 3:
{
if(Message( )==2) right( );
else
{
Off(OUT_A+OUT_C);
until(Message( )==3);
right( );
}

//

49 50

break;
}
case 4: StopAllTasks( );
}
ClearMessage( );
start check_turn_signal;
start trace;
}
task trace( )
{
while(true)
{
OnFwd(OUT_A+OUT_C);
if(SENSOR_2>THRESHOLD)
{
Float(OUT_C);
until(SENSOR_2<=THRESHOLD);
}
if(SENSOR_2<THRESHOLD)
{
Float(OUT_A);
until(SENSOR_2>=THRESHOLD);
}
}
}

//

task main( )
{
init( );
PlaySound(SOUND_UP);
Wait(100);
start check_obstacle_signal;
start check_turn_signal;
start trace;
}

//

C ROBOLAB

50 50

You might also like