This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
146 lines (125 loc) · 2.86 KB
/
commands.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
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "osObjects.h"
#include "commands.h"
#include "sonar.h"
#include "Comms.h"
#include <stdio.h>
#include "servo.h"
#include "motors.h"
/**
@brief Parse command to common format
*/
cmd_t parse_command(char * command_string){
cmd_t command;
char status;
status = sscanf(command_string, "@%i#%d#%d#%d$", (int *)&command.name, &command.arg1, &command.arg2, &command.arg3);
if (status != 4){
command.name = COMMAND_PARSE_FAILED;
}
return command;
}
void Command_STOP(void){
driveStop();
if(tid_zumoAI){
Kill_ZumoAI();
}
}
void Command_MOVE(direction_t direction, int32_t distance_cm){
drive(speed,direction,distance_cm);
}
void Command_TURN_LEFT(void){
turnLeft();
}
void Command_TURN_RIGHT(void){
turnRight();
}
void Command_TURN_BY_DEGREE(int angle){
rotate(angle);
}
void Command_SPEED_UP(void){
if (speed<=90){
speed+=10;
};
}
void Command_SPEED_DOWN(void){
if (speed>=10){
speed-=10;
};
}
void Command_SPEED_SET(int new_speed){
if (new_speed >= 0 && new_speed <= 100){
speed = new_speed;
};
}
void Command_SONAR_MODE(int NewMode){
switch (NewMode){
case 0:
ServoChangeMode(MANUAL);
ServoMoveByDegree(0);
break;
case 1:
ServoChangeSweepMode(SCAN_AND_GO);
ServoChangeMode(SWEEP);
break;
case 2:
ServoChangeSweepMode(SCAN_AND_LOCK);
ServoChangeMode(SWEEP);
break;
default:
break;
}
}
void Command_SONAR_SINGLE_POOL(int new_deg){
SendMessage("%04d,%04hu\n",new_deg,SonarGetDistance(new_deg));
}
void Command_SONAR_SINGLE_INT(int new_deg){
SonarStartMeas(new_deg);
}
void Command_SONAR_SET_LOCK_RANGE(int new_range){
ServoChangeLockRange(new_range);
}
void Command_COMMAND_PARSE_FAILED(void){
SendMessage("Unknown command.\n");
}
void ExecuteCommand(cmd_t * command){
switch (command->name){
case STOP:
Command_STOP();
break;
case MOVE:
Command_MOVE((direction_t)command->arg1,command->arg2);
break;
case TURN_LEFT:
Command_TURN_LEFT();
break;
case TURN_RIGHT:
Command_TURN_RIGHT();
break;
case TURN_BY_DEGREE:
Command_TURN_BY_DEGREE(command->arg1);
break;
case SPEED_UP:
Command_SPEED_UP();
break;
case SPEED_DOWN:
Command_SPEED_DOWN();
break;
case SPEED_SET:
Command_SPEED_SET(command->arg1);
break;
case SONAR_MODE:
Command_SONAR_MODE(command->arg1);
break;
case SONAR_SINGLE_POOL:
Command_SONAR_SINGLE_POOL(command->arg1);
break;
case SONAR_SINGLE_INT:
Command_SONAR_SINGLE_INT(command->arg1);
break;
case SONAR_SET_LOCK_RANGE:
Command_SONAR_SET_LOCK_RANGE(command->arg1);
break;
case COMMAND_PARSE_FAILED:
default:
Command_COMMAND_PARSE_FAILED();
}
}