COMP2401 - Assignment

COMP2401 - Assignment #5
(Due: Mon. Mar 25, 2019 @ 12 noon)
In this assignment, you will make a simulator for simple robots that uses multiple threads and allows
multiple robots to connect to it … with each robot running as its own process.
To begin this assignment, you should download the following files:
simulator.h – contains definitions and structs that will be used throughout your code
simulator.c – contains a template for the code that will run the simulator server
display.c – contains the window/drawing code that you will use to display the robots
stop.c – contains a template for the code for a process that will stop the simulator
robotClient.c – contains a template for the code for a process that will run a single robot
When compiling the files, you will need to include the -lm -lpthread and -lX11 libraries.
(but the -lX11 is only needed for the simulator.c file since it uses a window and graphics.)
Follow the steps below (in order) to complete the assignment:
(1) Examine the simulator.h file. The robots have a radius of ROBOT_RADIUS. Each robot is
represented as an (x,y) location and a direction. The x and y values are the location of the
robot in the window … which will be in the range from ROBOT_RADIUS to (ENV_SIZE -
ROBOT_RADIUS). The direction should always be in the range from -180° to +180°. Each
time a robot moves forward, it moves ROBOT_SPEED pixels in the direction that it is facing.
Each time a robot turns, it turns ±ROBOT_TURN_ANGLE degrees. The Environment
contains up to MAX_ROBOTS robots in an array. numRobots is the number of robots
currently registered with the server. The shutDown flag indicates whether or not the
environment has been shut down, it must be set to 0 upon startup.
(2) The simulator.c file contains the code for the server template. The main function in this file
MUST spawn two threads … one that will repeatedly accept incoming client requests … and
another that will repeatedly redraw the robots in the window. Both of these threads should
continue until the shutDown flag in the Environment has been set at which point the threads
will each exit gracefully. A pointer to the Environment should be passed in when spawning
the threads so that the threads have access to the robots and the shutdown flag.
Write the code so that the two threads call these two functions, respectively:
void *handleIncomingRequests(void *environment)
void *redraw(void *environment)
The redraw() function is in the display.c file and has been completed for you. You MUST
NOT alter any code in the display.c file. When you compile and run the simulator.c file, you
should see a window come up which is empty. Run it in the background (i.e., use &) and then
type ps in the terminal window to see the process id. When you close the window, you should
see some XIO error (don’t worry about this). Then typing ps, you should see that the simulator
is no longer running.
Write the code in the handleIncomingRequests() function so that it starts up the server and
repeatedly waits for incoming user requests. There are three possible incoming requests
defined by the definitions in the simulator.h file: (1) REGISTER, (2) STOP and (3) MOVE_TO.
For now, write code that accepts an incoming request to STOP the simulator. The code
should be based on the TCP version of the client/server sockets that were discussed in the
notes. The incoming command for STOP should come in as a single byte. Upon receiving it,
the server should shut down gracefully and both threads should be stopped cleanly.
Complete the code in the stop.c file so that it attempts to connect to the server and send the
STOP command to the server. Test your code by running the simulator server in the
background and then running the stop program. If all works well, the simulator window should
close and should shut down garecfully. Use ps to make sure that the simulator has indeed
shut down properly as well as the stop program. Make sure that you don’t have any
segmentation faults.
(3) Now add code to the robotClient.c file so that it attempts to register with the running
simulator server. To do this, it should send a REGISTER command byte . It should then
wait for a response from the server that should contain an OK or NOT_OK byte response. If
the response byte was OK, then additional bytes should be received containing the id of the
robot (i.e., its number in the environment array of robots), the randomly chosen (x, y) position
and the randomly chosen direction. Note that it is the simulator server that should choose the
random location and direction (see step (1) above for valid ranges). If you are setting up the
send/receive buffer as unsigned bytes, you will need to split the x, y and direction values into
high and low bytes. You may also want to have an extra byte to indicate the sign of the
direction (i.e., positive or negative) since the magnitude will be 0 to 180. You will need to
adjust the handleIncomingRequests() function in the simulator.c file so that it sends the
appropriate bytes back to the client. When all is working, you should see the robot appear in
the simulator window. You’ll need to run the simulator first and then run the robotClient
process. Once it works, try running a second and third robotClient process … you should
see 2 and then 3 robots appearing. Make sure that the stop process still shuts down the
simulator properly.
(4) There is a limit to how many robots that can be added. It is set as MAX_ROBOTS, which is 20
by default. Adjust the code in the simulator server to deny any registrations that go beyond
this limit. Simply send a NOT_OK response when it is full to capacity. Make sure that the
robotClient handles this response properly. Test everything by adding 20 robots and then try
adding a 21st robot. Make sure that you display an appropriate error message in the
robotClient code so that it is clear when the client is unable to register.
(5) Now add functionality to the robotClient so that it repeatedly moves the robot around in the
environment indefinitely. To move forward, the robot should calculate a new location based
on its current location and direction as follows: (newX, newY) = (x+S*cos(d), y+S*sin(d))
where (x,y) is the current robot location, S is the robot’s speed (in pixels) and d is the robots
direction. The robot should then send the MOVE_TO request to the server simulator which
should contain the following information:
the MOVE_TO command
the robot’s ID
the new location that the robot would like to move to
the robot’s direction
Keep in mind that if you use unsigned char array to send the bytes, you will need to break
down the newX, newY into two bytes (most significant byte and least significant byte). You
will also need to be careful sending the direction (which is ±180°) since an unsigned char only
stores values in the 0-255 range and a signed char in the -128 to +127 range.
The robot should receive a single byte back from the server which has one of 3 values:
1. OK if the robot can move to that location without problems,
2. NOT_OK_BOUNDARY if the robot cannot move to that location because it would go
outside the boundary of the environment, and
3. NOT_OK_COLLIDE if the robot cannot move to that location because it would collide
with another robot.
If all is OK, the robot’s (x,y) location should be updated at the client end. Otherwise, the robot
should turn left or right (chosen randomly) by ROBOT_TURN_ANGLE degrees. To do this, do
not change the (x,y) location, just change the direction. Make sure that the direction always
remains in the ±180° range. Also, set your code up so that when the robot first hits a boundary
or collides with another robot, it computes the random direction to turn towards (CW or CCW).
If the robot is unable to move on any successive turns, it should continue to turn in the SAME
direction again. It should keep turning ROBOT_TURN_ANGLE degrees once per loop
iteration. Only when it is OK to move forward should it begin its forward movement again.
Coding things in this manner will ensure that the robot does not alternate turning back and
forth in what appears to be an indecisive pattern. If coded properly, you should see the robot
turning little by little upon collision.
(6) To make things work, you will need to adjust the handleIncomingRequests() function in the
simulator.c file so that it handles incoming MOVE_TO requests from the robots. It will need
to receive all the incoming data that was sent to it as part of the MOVE_TO request. It will
then need to ensure that the new location request is valid. Write a canMoveTo() function that
determines whether or not the robot can move to that location without collision. It will need to
check the boundary values as well as the locations of all other robots to decide whether to
return a value of OK, NOT_OK_BOUNDARY or NOT_OK_COLLIDE. Test your code with a
single robot to see if it works properly. You can lengthen the usleep() delays to slow things
down and investigate the movements, but you must put them back to their initial values when
things seem to be working properly. Test everything with multiple robots and ensure that the
moving and collisions are working properly. You might end up with robots stuck on each other
… see the next part as a possible explanation,
(7) When registering many robots, each is given an initial start location. Sometimes, the location
of one robot may overlap with the location of another robot and the two can get stuck together.
Adjust the handleIncomingRequests() function in the simulator.c file so that it ensures that
each robot is placed a unique non-overlapping location in the environment. That is, make
sure that the robot can “move to” (i.e., or be initially placed at) the randomly chosen location.
You might want to use a WHILE loop until a good random location is found.
________________________________________________________________________________
IMPORTANT SUBMISSION INSTRUCTIONS:
Submit all of your c source code files as a single tar file containing:
1. A Readme text file containing
your name and studentNumber
a list of source files submitted
any specific instructions for compiling and/or running your code
2. All of your .c source files and all other files needed for testing/running your programs.
3. Any output files required, if there are any.
The code MUST compile and run on the course VM, which is COMP2404B-W19.
If your internet connection at home is down or does not work, we will not accept this as a reason for
handing in an assignment late ... so make sure to submit the assignment WELL BEFORE it is due !
You WILL lose marks on this assignment if any of your files are missing. So, make sure that you hand
in the correct files and version of your assignment. You will also lose marks if your code is not written
neatly with proper indentation and containing a reasonable number of comments. See course
notes for examples of what is proper indentation, writing style and reasonable commenting).

因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

微信:codinghelp

原文地址:https://www.cnblogs.com/helpyourjava/p/10595492.html

时间: 2024-11-11 09:15:32

COMP2401 - Assignment的相关文章

COMP2401

COMP2401 - Assignment #4(Due: Sun. Mar 15, 2020 @ 6pm)In this assignment, you will gain practice dynamically allocating/freeing memory as well as workingwith pointers to allocated structures. You will also use a makefile to compile the code. You will

Algorithm Part I:Programming Assignment(2)

问题描述: Programming Assignment 2: Randomized Queues and Deques Write a generic data type for a deque and a randomized queue. The goal of this assignment is to implement elementary data structures using arrays and linked lists, and to introduce you to g

RailsCast26 Hackers Love Mass Assignment rails中按params创建、更新model时存在的安全隐患

Mass assignment是rails中常用的将表单数据存储起来的一种方式.不幸的是,它的简洁性成了黑客攻击的目标.下面将解释为什么及如何解决. 上述表单为一个简单的注册表单.当用户填入name,点击提交时,一个新用户被创建.用户模型被如下定义: ruby create_table :users do |t| t.string :name t.boolean :admin, :default => false, :null => false end 当用户点击提交时,如下的action被执

hdu4781 Assignment For Princess(构造)

题目链接:hdu4781 Assignment For Princess 题意:n个点m条边,每条有向边的权值分别是1,2,3…m,一个点能到达任意一个点,没有重边和自环,没有任何两条边的权值相同,任意一个有向环的权值和必须是3的倍数,现在需要把这个图输出来. 题解:注意到题目给出的范围m >= n+3,所以一定是可以构造出一个1~n的回路使得权值和为3的倍数的,可以让前n-1条边权值为1~n-1,第n条边(n->1)可以为n, n+1, n+2从而满足题意,后面再连任意两条不相邻的边时,边权

<Effective C++>读书笔记--Ctors、Dtors and Assignment Operators

<Item 5> Know what functions C++ silently writes and calls 1.If you don't declare them yourself, compilers will declare their own versions of a copy constructor, a copy assignment operator, and a destructor. Furthermore, if you declare no constructo

POJ 3189 Steady Cow Assignment(最大流)

POJ 3189 Steady Cow Assignment 题目链接 题意:一些牛,每个牛心目中都有一个牛棚排名,然后给定每个牛棚容量,要求分配这些牛给牛棚,使得所有牛对牛棚的排名差距尽量小 思路:这种题的标准解法都是二分一个差值,枚举下界确定上界,然后建图判断,这题就利用最大流进行判断,值得一提的是dinic的效率加了减枝还是是卡着时间过的,这题理论上用sap或者二分图多重匹配会更好 代码: #include <cstdio> #include <cstring> #inclu

hdu 1845 Jimmy’s Assignment (二分图)

Jimmy's Assignment Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 896    Accepted Submission(s): 379 Problem Description Jimmy is studying Advanced Graph Algorithms at his university. His most

FreeRTOS的application assignment

Please follow the steps precisely in order to complete the objectives of the assignment. If you use the C++ FreeRTOS framework, it should make the assignment significantly easy. Create a producer task that takes 1 light sensor value every 1ms. Collec

用户无法进入SDSF,报NO GROUP ASSIGNMENT错误

注:命令行小写部分表出需要根据自己的情况改变!! a)激活SDSF资源类 SETROPTS CLASSACT(SDSF) b)查看SDSF资源类的PROFILE RLIST SDSF * c)如果不存在GROUP.ISFUSER.servername的PROFILE,则需要定义, RDEFINE SDSF (GROUP.ISFUSER.servername) OWNER(userid or group name) UACC(READ) 这样,所有用户都可以有读取权限,就都可以访问SDSF了 附1