Header Files RobotC????

  1. 8 months ago

    Best

    8 Oct 2017 Surrey, BC 4549B

    So I want to know how to use header files in robotC. Do I have to save all my files as header files first except main.c and #include them as xyz.h? Or do I keep the files as .c files then #include them as xyz.c?

    @Best what is the difference between sleep and wait1msec.

    sleep, wait1msec and delay are all exactly the same.

    @Best what do you mean by "// call the function that you included at the top (void drive() { ... })"?

    #include "drive.c" literally just copy pastes the contents of drive.c into main.c. In this case the contents of drive.c is a function called "drive". The usercontrol code in my example runs (calls) each function (drive, lift, mogo, etc.) multiple times a second.

    @Best do I just upload the main file to the cortex or all the files?

    Just the main file. Before the upload starts the main file will #include all the files you want and the robot will just see one big file.

  2. DarkMatterMatt

    9 Oct 2017 Auckland, New Zealand 2900C

    Header files are not generally used in RobotC (because RobotC is an intentionally simplified Frankenstein of C/C++). I would suggest you keep the .c extension and #include "xyz.c" like the competition template does.

    You can include files from a different directory by adding a "Directory for User Include Files (common)." View > Preferences > Detailed preferences > Menu level = Expert > Compiler > Include directories.

    https://stackoverflow.com/questions/1695224/what-do-c-and-h-file-extensions-mean-to-c
    https://stackoverflow.com/questions/3482948/any-fundamental-difference-between-source-and-header-files-in-c

  3. Best

    9 Oct 2017 Surrey, BC 4549B

    Will this work. I attached an image.

  4. DarkMatterMatt

    10 Oct 2017 Auckland, New Zealand 2900C
    Edited 8 months ago by DarkMatterMatt

    You still need task autonomous, pre_auton & usercontrol. Unless you have very large sections of code I would suggest putting it all in one file, that way it's easier to find everything (you don't need to keep flicking between 6+ tabs).

    drive.c, claw.c, mogo.c, etc.:

    void drive() {
        // do stuff to do with drive. This will be called 50 times a second in usercontrol
    }

    main.c:

    #pragma config(Motor,  port5,           driveLeft,     tmotorVex393_MC29, openLoop, driveLeft)
    #pragma config(Motor,  port6,           driveRight,    tmotorVex393_MC29, openLoop, reversed, driveRight)
    // lift, mogo, other ports as well
    //*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
    
    // This code is for the VEX cortex platform
    #pragma platform(VEX2)
    
    // Select Download method as "competition"
    #pragma competitionControl(Competition)
    
    //Main competition background code...do not modify!
    #include "Vex_Competition_Includes.c"
    
    #include "lift.c"
    #include "drive.c"
    #include "turn.c" // what is this? It sounds like it should be in drive.c
    #include "claw.c"
    #include "chainbar.c"
    #include "mogo.c"
    
    void pre_auton() {
    
    }
    
    task autonomous() {
    
    }
    
    task usercontrol() {
    	while (true) {
    		sleep(20); // only run 50 times a second
    		lift();
    		drive(); // call the function that you included at the top (void drive() { ... })
    		turn();
    		claw();
    		chainbar();
    		mogo();
    	}
    }

    EDIT: also please copy paste your code into [ code] ... [ /code] (no spaces) blocks instead of posting pictures :P It means we don't need to type up your code again for examples.

  5. Best

    10 Oct 2017 Surrey, BC 4549B
    Edited 8 months ago by Best

    Oh sorry I didn't know how to put the code without taking a picture. And also, what is the difference between sleep and wait1msec. And what do you mean by "// call the function that you included at the top (void drive() { ... })"?

    And do I just upload the main file to the cortex or all the files?

  6. DarkMatterMatt

    10 Oct 2017 Answer Auckland, New Zealand 2900C

    @Best what is the difference between sleep and wait1msec.

    sleep, wait1msec and delay are all exactly the same.

    @Best what do you mean by "// call the function that you included at the top (void drive() { ... })"?

    #include "drive.c" literally just copy pastes the contents of drive.c into main.c. In this case the contents of drive.c is a function called "drive". The usercontrol code in my example runs (calls) each function (drive, lift, mogo, etc.) multiple times a second.

    @Best do I just upload the main file to the cortex or all the files?

    Just the main file. Before the upload starts the main file will #include all the files you want and the robot will just see one big file.

 

or Sign Up to reply!