ROB 599: Programming for Robotics: Class 2

Getting started with the class submission system

We will be using an automated in-class problem and homework submission system. The system will allow you to get instant feedback on the correctness of your solution and will make it easy for us to compare our solutions and learn together from them in class.

Initial gitlab repository setup

  • Open up a terminal
  • You should be in your home directory, indicated by the prompt ending with ~$
  • We will be using git to work with our code in this class. Git is a version control system, meaning that it helps you keep track of all the changes you make to your code and commit over time. This way you can feel easy about experimenting with your code because old versions of it will be kept safe.
  • We will be using the EECS Gitlab at https://gitlab.eecs.umich.edu to hold and access your git code repository. Make sure you can log in to this website.
  • Next we need to give your computer automatic access to your gitlab account. We will be using an SSH (secure shell) key to do this. This key is essentially a file that holds a very long password (private key) and another file that holds a very long username (public key).
  • Below this paragraph we have two commands on separate lines. When you see commands in this font, you should copy and paste the lines one by one into the terminal to run. To paste to the terminal you need to use Ctrl-Shift-V. Replace uniqid with your own uniqid and hit enter several times to accept the default options. You don’t want to add a password here because that will make using the key extremely inconvenient.
    ssh-keygen -t ed25519 -C "uniqid@umich.edu"
    

    Then you can run this command to read the public key that was just made:

    cat ~/.ssh/id_ed25519.pub
    

    Use Ctrl-Shift-C to copy the output of the cat command. It should start with ssh-ed25519 and end with your email address. Paste it into the ‘Key’ text box of this page: https://gitlab.eecs.umich.edu/profile/keys and hit ‘Add key’. Verify that it is working by running:

    ssh -T git@gitlab.eecs.umich.edu
    

    If it doesn’t work the first time, please get someone else to check your steps because if you make more than one or two errors here you will get temporarily blocked!

  • We are providing a git repository template that will provide a common directory structure for automatic grading of problems and assignments. Clone this repository to your computer with:
    git clone https://umbrella.eecs.umich.edu/acshikh/p4robotics-templates.git rob599
    
  • Move into the repository folder with cd rob599
  • Right now this is only a clone of the class template. You need to make it into a real repository of your own on gitlab. We do this by changing the origin (of course, use your own uniqid):
    git remote set-url origin git@gitlab.eecs.umich.edu:uniqid/rob599.git
    
  • So that we can still keep track of changes made to the original template, we call it upstream and tell git to remember where it is:
    git remote add upstream https://umbrella.eecs.umich.edu/acshikh/p4robotics-templates.git
    
  • Now run git push origin master and your repository will be created on gitlab!
  • The repository also includes a configuration script we need to run:
    bash scripts/p4r-env-setup.sh
    source ~/.bashrc
    

    We use source ~/.bashrc to immediately make the changes to bash’s configuration take effect. This means we will be able to immediately use the class scripts we have installed instead of needing to close and reopen the terminal to have access to them.

  • For the auto-grade system to work, it needs to have permission to pull from your repository. Navigate to https://gitlab.eecs.umich.edu/uniqid/rob599/project_members (with your uniqid!) and add acshikh as a “reporter”.

Starting the class2 assignments: helloworld

In this problem we will write the classic “helloworld” program to demonstrate the workflow we will be using for all the assignments in this class:

  1. Write your code and makefile
  2. Compile and test it on your computer
  3. Use git to add and commit your files
  4. Use git to push your code online
  5. Use p4r-submit to evaluate your code
  6. Repeat back to 1 until you are satisfied with your score

We will first walk you through all of the minute steps for this first problem:

  • Change into the class2 folder of the repository with cd class2
  • Your prompt should look something like ~/rob599/class2$
  • Open the class2 directory in your choice of code editor. I recommend either Visual Studio Code or Atom, but you can use whichever editor you like! Both Visual Studio Code and Atom are already available on the CAEN computers.
  • Make a file called helloworld.c and enter the following:
int main(void) {
    return 0;
}
  • Make a file called makefile and enter the following. Note that the indentation in makefiles must use tabs and not spaces, and that copying from this online page will give you spaces! This makefile is specifying how to make a file called helloworld. The first line says that our program helloworld has one source code file: helloworld.c. The second line says that to compile our code, we should run gcc -o helloworld helloworld.c -std=c11. We are using the gcc compiler, and the -o option tells gcc that our program will be called helloworld. The -std=c11 tells the compiler we are using modern C. The remainder of the line are the source code files to compile.
helloworld: helloworld.c
    gcc -o helloworld helloworld.c -std=c11
  • Run make helloworld. This creates (i.e. ‘makes’) an executable helloworld from your code. Run ./helloworld to confirm it has been made. It shouldn’t actually do anything when run.
  • In order to submit your problem solution, we have to use git:
    • Run git add helloworld.c makefile to add these new files to your repository. (you can list as many files as you like).
    • Run git status to see that these two files are now ready “to be committed”.
    • Run git commit -m "helloworld basic code files" (or with whatever message you like) to commit this code to your own local git repository. You can put whatever message you want, but you do need to include a message.
    • Run git status again and notice that your files are no longer mentioned as modified or needing to be committed.
    • Run git push to send your commits up to the eecs gitlab repository we made earlier.
  • We have installed a script called p4r-submit to make submission and evaluation of code easy. Run p4r-submit helloworld to ask the class server to pull your github repository and evaluate the helloworld problem. It knows which class or homework problem set to evaluate by checking the current directory of your terminal. If you just run p4r-submit without specifying a problem, it will evaluate all the problems in the current assignment.
  • You should get a response from p4r-submit about how much of the problem solution was correct. It should say you had the correct exit/status code of 0, but that it failed to output the correct string, which is "hello world\n" (\n is a new line character). It should also note that you have passed a Valgrind test, which checks for memory violations, and a style check, which enforces a class coding style.
  • Fix your program so that it prints out "hello world\n". You can refer to this tutorial document for a review of the C programming language.
  • Run git status. You should see that it recognizes your helloworld.c file has been modified.
  • Run git add -u to add all the files that git status had listed as modified.
  • Run commit (with git commit -m "your message"), push (git push), and resubmit (p4r-submit helloworld). You should now get full points for this problem.

In-class problem 2: fizzbuzz

This is a classic beginner problem: output the numbers 1 to 100, one number on each line. If the number is divisible by 3, instead output fizz. If the number is divisible by 5, instead output buzz. If divisible by both, output fizzbuzz. The last character of output should be a new line.

Starting like so:

1
2
fizz
4
buzz
...

If your source file is called fizzbuzz.c you can actually run make fizzbuzz to compile it to an executable called fizzbuzz without having to change your makefile! This is because make has certain default “rules” about how to compile c code. However, it is much better to be explicit. One thing we can do to make editing the make file simpler, is to use certain make variables. $@ will be replaced with the name of the “target” (what comes before the colon), and $^ will be replaced with the list of dependencies (that come after the colon). So we only need to add the following to our makefile:

fizzbuzz: fizzbuzz.c
    gcc -o $@ $^ -std=c11

which means the whole file will look like:

helloworld: helloworld.c
    gcc -o helloworld helloworld.c -std=c11

fizzbuzz: fizzbuzz.c
    gcc -o $@ $^ -std=c11

In-class problem 3: primes

In this problem, your program will prompt the user to enter a number. Your program will then print out “Prime factors:” and the prime factors of that number in ascending order, one on each line, including repeats. For example, the full output from one run where the user typed 30 might look like the following:

Enter a number to factorize:
30
Prime factors:
2
3
5

We have provided a stub program that reads the number in from the user, so you just need to implement the algorithm.

If you choose to use any math functions such as sqrt in your code, you will notice that even when your code seems to compile it fails to link, saying something about undefined references. If this happens, you may need to instruct the compiler to link your program with a math library that provides the actual code for those functions. You can do this by adding -lm to the end of one of your gcc commands. -l stands for link and m for math.