-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensoryPerceptive.c
133 lines (114 loc) · 5.05 KB
/
SensoryPerceptive.c
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
#pragma config(Sensor, S1, touchFrontSensor, sensorTouch)
#pragma config(Sensor, S2, touchUpSensor, sensorTouch)
#pragma config(Sensor, S3, liteSensor, sensorLightActive)
#pragma config(Sensor, S4, soundSensor, sensorSoundDB)
#pragma config(Motor, motorA, , tmotorNXT, openLoop)
#pragma config(Motor, motorB, leftWheel, tmotorNXT, PIDControl, encoder)
#pragma config(Motor, motorC, rightWheel, tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
Decision making rule to decide between 3 sensors.
Uses the rand function to make the decision somewhat random.
It will tend to respond to the light sensor in brighter lights
It will tend to respond to the sound sensor when it is loud
It will tend to respond to the sonar sensor when it is close to something
*/
task main()
{
int lite;
int sound;
int touchUp;
int touchFront;
int speed=0;
int range;
int choice;
while (true) {
// get sensor values, and multiply by time since
lite = SensorValue(liteSensor);
sound = SensorValue(soundSensor);
touchUp = SensorValue(touchUpSensor)*1000;
touchFront = SensorValue(touchFrontSensor)*1000;
// sum the sensor values
range = lite + sound + touchUp + touchFront;
//create random value between zero and range
choice = abs(rand()) % range; // choice is between 0 and range
if (choice < touchFront) { //WALL OPTION
if (random(1) < 1) {
motor[leftWheel] = -50; // turn right in reverse
motor[rightWheel] = -10;
}
else {
motor[leftWheel] = -10; // turn left in reverse
motor[rightWheel] = -50;
}
wait1Msec(750);
nxtDisplayTextLine(2, "WALL REACHED"); // print choice
}
else if (choice < lite + touchFront) { //LIGHT OPTION
speed = SensorValue[liteSensor];
motor[leftWheel] = speed;
motor[rightWheel] = speed;
nxtDisplayTextLine(2, "LIGHT"); // print choice
}
else if (choice < lite + sound + touchFront) {
if (SensorValue[soundSensor] > 65) { // SCARED OPTION
nxtDisplayTextLine(2, "SCARED BY SOUND"); // print choice
PlaySound(soundDownwardTones);
motor[leftWheel] = 100;
motor[rightWheel] = 100;
wait1Msec(250);
motor[leftWheel] = 0;
motor[rightWheel] = 0;
wait1Msec(100);
motor[leftWheel] = SensorValue[soundSensor];
motor[rightWheel] = -SensorValue[soundSensor];
wait1Msec(1000);
motor[rightWheel] = speed;
motor[leftWheel] = speed; // resume last speed set
} else if (SensorValue[soundSensor] > 40
&& SensorValue[soundSensor] <= 65) { // INTRUIGED OPTION
nxtDisplayTextLine(2, "INTRUIGED BY SOUND"); // print choice
motor[leftWheel] = -speed;
motor[rightWheel] = -speed; // step back briefly
wait1Msec(500);
motor[leftWheel] = speed; // resume last speed
motor[rightWheel] = speed;
} else { // MEH OPTION
nxtDisplayTextLine(2, "MEH"); // print choice
motor [leftWheel]= SensorValue[liteSensor];
motor [rightWheel]= SensorValue[liteSensor];
}
}
else {
if (SensorValue[soundSensor] > 50) { // STARTLED BY TOUCH OPTION
nxtDisplayTextLine(2, "STARTLED BY TOUCH"); // print choice
motor[leftWheel] = 100;
motor[rightWheel] = 100;
wait1Msec(250);
motor[leftWheel] = 0;
motor[rightWheel] = 0;
wait1Msec(100);
motor[rightWheel] = speed; // resume last speed set
motor[leftWheel] = speed;
} else { // PURR AND DANCE OPTION
nxtDisplayTextLine(2, "PURR AND DANCE"); // print choice
motor[leftWheel] = 0;
motor[rightWheel] = 0;
for (int i=0; i<5; i++) { // "dance" is a random set of motor speeds ranging from -50 to 50
PlaySound(soundLowBuzz); // "purr" is a low buzz
motor[leftWheel] = random(100)-50;
motor[rightWheel] = random(100)-50;
wait1Msec(1000);
}
motor[leftWheel] = speed; //resume last speed set
motor[rightWheel] = speed;
}
}
nxtDisplayTextLine(1, "CHOICE:");
nxtDisplayTextLine(3, "light=%i", lite); // print values
nxtDisplayTextLine(4, "sound=%i", sound);
nxtDisplayTextLine(5, "touchUp=%i", touchUp); // print values
nxtDisplayTextLine(6, "touchFront=%i", touchFront);
wait1Msec(1000); // wait one second before sampling again
}
}