CS 1713 Section 3, Spring 1997
Assignment 4: Loops and Files: Mars Mission
For this assignment, you will write a program that checks a plan for
maneuvering a vehicle around an obstacle to a target, given a
limited amount of fuel.
The Mars Land Rover Mission
An unmanned rocket has been launched to the planet Mars to study the red planet.
The plan was that the rocket would release a lander, which would land on
the Martian surface. From the lander, an automated land rover would be released
and would drive around, gathering and analyzing rock samples nearby,
and sending its results back to the Earth via the orbiting rocket.
The the chosen landing site is an area 100 kilometers due east of Olympus
Mons, an enormous volcano. This area was specifically targeted by
scientists because of interesting geological phenomenae.
The Problem
Unfortunately, but typically, the engineers on
the project were holding their map upside down and thus accidentally caused
the lander to land 100 km due west of Olympus Mons. The rover
must analyze rocks from within 5 km of the intended landing
site or the mission will be a failure and the engineers will be
sent to the hell currently occupied by the Hubble space telescope engineers.
The engineers
have decided to send commands to the land rover to drive it over to the
target site. They will have to steer the rover around the volcano since
it is surrounded by a steep circular escarpment (cliff) 450 km
in diameter.
The engineers move the rover by sending it commands to
turn by a certain number of degrees
or move forward a certain number of kilometers. The rover has a tank
filled with 15 liters of fuel, and can go 70 km per liter over
the rough Martian terrain (turning consumes no fuel).
They would like to move the rover to the site and have some
fuel left over for a little exploration.
Here is an ASCII drawing of the situation,
in a coordinate system where the y axis points
north, the x axis points east, and the origin is the lander.
########
###############
##################
#####################
#######################
##### Olympus Mons ####
(0,0) ######################### (750,0)
*|<--100 km-->|##<-------450 km------>##|<--200 km-->|X
Lander ######################### Target
#######################
#######################
#####################
##################
###############
########
The rover is guided by a plan, or set of commands and parameters.
The turn command is given by the character r (for rotate) and the
move forward command is given by the character f. For example,
consider the following plan:
f 99
r 90
f 225
r -90
f 552
r -90
f 225
r 90
f 100
This says to move forward (initially east) 99 km, stopping 1 km
from the volcano, turn right 90 degrees, move forward (now south) 225 km,
turn left 90 degrees (i.e., rotate -90 degrees), move forward 552 km,
passing to the south of
the volcano, turn left 90 degrees, move forward 225 km, turn right 90 degrees
then move forward 100 km.
The rover would now be at coordinate (751, 0), within 5 km of the
target, which is at (750, 0). However, this isn't such a good plan, since
the rover has to go a total of 99+225+552+275+100 = 1201 km, exhausting
its fuel supply (1201km / 70km/L is about 17 liters) before reaching
the target.
The engineers have proposed several plans for maneuvering the rover to
the site. They don't know which plan is the best.
They need to check the plans to see which ones will work and which one has
the most fuel left over at the end. They need a simulator for the rover
that will tell them whether a given plan will bump into the volcano,
run out of fuel, miss the target by more than 5 km, etc.
The Assignment
Your job is to write a program called rover.c that simulates the
rover as it follows a plan given in a file. The filename should be a command
line argument, so if the name of a plan were plan, you would invoke
your program with "rover plan". Your program should:
- Read commands from the file until the end of file.
- Echo each command it reads
- After each command, print the current location (x,y coordinates),
and orientation (degrees) in the rover in the coordinate system, and the
amount of fuel left.
- Print a message if the rover tries to drive over the volcano.
- Print a message if the rover runs out of fuel.
- At the end, report how much fuel is left and whether the rover reached
within 5 km of the target.
For example, on the plan presented above, your program should produce output
similar to this:
command: forward 99.00
position: ( 99.00, 0.00) orientation: 0.00 fuel: 13.59
command: rotate 90.00
position: ( 99.00, 0.00) orientation: 90.00 fuel: 13.59
command: forward 225.00
position: ( 99.00, 225.00) orientation: 90.00 fuel: 10.37
command: rotate -90.00
position: ( 99.00, 225.00) orientation: 0.00 fuel: 10.37
command: forward 552.00
position: (651.00, 225.00) orientation: 0.00 fuel: 2.49
command: rotate -90.00
position: (651.00, 225.00) orientation: -90.00 fuel: 2.49
command: forward 225.00
position: (651.00, 0.00) orientation: -90.00 fuel: -0.73
command: rotate 90.00
position: (651.00, 0.00) orientation: 0.00 fuel: -0.73
command: forward 100.00
position: (751.00, 0.00) orientation: 0.00 fuel: -2.16
out of fuel!
within 5 kilometers; ok to take sample.
On runner, in the directory ~djimenez/cs1713, there are
four plan files, plan1 through plan4; run your
program on these files and make sure it works. Make sure your
source code is throroughly commented. To turn in the program,
e-mail your source code to djimenez@ringer.cs.utsa.edu
with subject "CS1713 Assignment 4".
Contest for Extra Credit
For extra credit, design and test your own plan for getting the
rover to within 5 km of the target. The student with the
best plan will be awarded 5 extra points on this program; the
second and third best will be awarded 3 extra points. "Best"
means with the most fuel left over upon reaching the target according
to the instructor's version of the program; in the
event of a tie, the plan that arrives closer to the target wins.
If it is still a tie, the first student to turn in the tied plan
wins. Turn in your plan in a separate e-mail to
djimenez@ringer.cs.utsa.edu with subject "Plan contest".
Programming Issues
-
How can you open a file given on the command line?
The parameters argc and argv are passed to your
main() function; you can accept them like this:
#include <stdio.h>
int main (int argc, char *argv[]) {
This says that main accepts an integer called argc
and an "array of pointers to characters" called argv. argc
is the number of command line arguments and argv is the arguments
themselves. You can then open a file whose name is on the command line like this:
FILE *f;
if (argc == 2) {
f = fopen (argv[1], "r");
if (!f) /* give an error message, file is bad */
} else /* give an error message, argc is wrong */
- How can I tell how far the rover is from the escarpment?
Olympus Mons can be thought of as a circle at the point (325,0)
with radius 225. If the current x,y position comes within 275
kilometers of (325,0), then the rover is trying to go over the escarpment.
Note that it's OK to be right up against the escarpment, e.g.,
at point (100,0). The metric here is two-dimensional Euclidean
distance from trigonometry.
- How can I tell how to change the x,y coordinate given an angle
and a distance? Think of the system as a right triangle, e.g.:
^
| ____o____
| |_| /
| | /
y | /
| a| /
a | / h
x | /
i | /
s |/ <--- theta
| *
|
<-|------x-axis------------>
The * corresponds to the position of the rover, theta is the
angle giving the
orientation of the rover, and h is the distance you want to move
forward. Figure out the values for a and o from h
and theta, then add a to
your current y position,
and o to your current x position. Note that the initial
orientation,
due east, corresponds to an angle of theta=90 degrees
(pi/2 radians).
- How do I know which direction to go? Why shouldn't the rover
move backwards instead of forwards? You can think of the rover as
being the center of a unit circle, moving towards the point on the
circle at the current angle. Don't make it harder than it really is :-)
This assignment is due Monday, February 17, 1997 at midnight.