回复至:小白问题:关于Cpp代码规范性 .h与.cpp文件的关系与内容的存疑

ygdm123
  • 文章数量: 25
@ygdm123
楼主

config.h

_Pragma("once")
#pragma once
#ifndef _CONFIG_H_
#define _CONFIG_H_

#include "main.h"

//---Declare Devices---
//Ctrl
Controller Ctrl1(CONTROLLER_MASTER);
Controller Ctrl2(CONTROLLER_PARTNER);

//Motor fwd
Motor LF(12,E_MOTOR_GEARSET_18,false);
Motor LB(13,E_MOTOR_GEARSET_18,false);
Motor FL(15,E_MOTOR_GEARSET_18,false);
Motor BL(16,E_MOTOR_GEARSET_18,false);
Motor ML(19,E_MOTOR_GEARSET_18,false);
//Motor rev
Motor RF(17,E_MOTOR_GEARSET_18,true);
Motor RB(18,E_MOTOR_GEARSET_18,true);

//Imu
Imu IS (14);

//3-Wire LED
ADIDigitalOut LED ('A');

#endif

ctrl_func.h

#pragma once
#ifndef _CTRL_FUNC_H
#define _CTRL_FUNC_H

#include "main.h"

void STOP_MOVE();
void STOP_LIFT();
void LOCK();
void Ctrl_L(int ctrl_power);

#endif

ctrl_func.cpp

#include "main.h"
#include "config.h"

long long shin_count = 0;
void Ctrl_Shin(bool status) {
  shin_count++;
  if (status) {
    if (shin_count % 10 == 0) {
      LED.set_value(1);
    }
    if (shin_count % 10 == 5) {
      LED.set_value(0);
    }
  } else {
    LED.set_value(0);
  }
}

void STOP_MOVE() {
  LF.set_brake_mode(E_MOTOR_BRAKE_BRAKE);
  LB.set_brake_mode(E_MOTOR_BRAKE_BRAKE);
  RF.set_brake_mode(E_MOTOR_BRAKE_BRAKE);
  RB.set_brake_mode(E_MOTOR_BRAKE_BRAKE);
}

void STOP_LIFT() {
  FL.set_brake_mode(E_MOTOR_BRAKE_HOLD);
  ML.set_brake_mode(E_MOTOR_BRAKE_HOLD);
  BL.set_brake_mode(E_MOTOR_BRAKE_HOLD);
}

void LOCK() {
  STOP_MOVE();
  STOP_LIFT();
}

void Ctrl_L(int ctrl_power) {
  if (ctrl_power == 0) {
    LF.set_brake_mode(E_MOTOR_BRAKE_BRAKE);
    LB.set_brake_mode(E_MOTOR_BRAKE_BRAKE);
  } else {
    LF.move(ctrl_power);
    LB.move(ctrl_power);
  }
}

main.cpp

#include "main.h"
#include "config.h"
#include "ctrl_func.h"

/**
 * Runs initialization code. This occurs as soon as the program is started.
 *
 * All other competition modes are blocked by initialize; it is recommended
 * to keep execution time for this mode under a few seconds.
 */
void initialize() {
       LED.set_value(1);
       LF.set_zero_position(0);
  LB.set_zero_position(0);
  RF.set_zero_position(0);
  RB.set_zero_position(0);
  FL.set_zero_position(0);
  BL.set_zero_position(0);
  ML.set_zero_position(0);
  IS.reset();
       LED.set_value(0);
}

/**
 * Runs while the robot is in the disabled state of Field Management System or
 * the VEX Competition Switch, following either autonomous or opcontrol. When
 * the robot is enabled, this task will exit.
 */
void disabled() {
       LOCK();
}

/**
 * Runs after initialize(), and before autonomous when connected to the Field
 * Management System or the VEX Competition Switch. This is intended for
 * competition-specific initialization routines, such as an autonomous selector
 * on the LCD.
 *
 * This task will exit when the robot is enabled and autonomous or opcontrol
 * starts.
 */
void competition_initialize() {}

/**
 * Runs the user autonomous code. This function will be started in its own task
 * with the default priority and stack size whenever the robot is enabled via
 * the Field Management System or the VEX Competition Switch in the autonomous
 * mode. Alternatively, this function may be called in initialize or opcontrol
 * for non-competition testing purposes.
 *
 * If the robot is disabled or communications is lost, the autonomous task
 * will be stopped. Re-enabling the robot will restart the task, not re-start it
 * from where it left off.
 */
void autonomous() {}

/**
 * Runs the operator control code. This function will be started in its own task
 * with the default priority and stack size whenever the robot is enabled via
 * the Field Management System or the VEX Competition Switch in the operator
 * control mode.
 *
 * If no competition control is connected, this function will run immediately
 * following initialize().
 *
 * If the robot is disabled or communications is lost, the
 * operator control task will be stopped. Re-enabling the robot will restart the
 * task, not resume it from where it left off.
 */
void opcontrol() {
       Controller master(E_CONTROLLER_MASTER);
       Motor left_mtr(1);
       Motor right_mtr(2);

       while (true) {
              lcd::print(0, "%d %d %d", (lcd::read_buttons() & LCD_BTN_LEFT) >> 2,
                               (lcd::read_buttons() & LCD_BTN_CENTER) >> 1,
                               (lcd::read_buttons() & LCD_BTN_RIGHT) >> 0);
              int left = master.get_analog(ANALOG_LEFT_Y);
              int right = master.get_analog(ANALOG_RIGHT_Y);

              left_mtr = left;
              right_mtr = right;
              delay(10);
       }
}

main.h

/**
 * file main.h
 *
 * Contains common definitions and header files used throughout your PROS
 * project.
 *
 * Copyright (c) 2017-2021, Purdue University ACM SIGBots.
 * All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef _PROS_MAIN_H_
#define _PROS_MAIN_H_

/**
 * If defined, some commonly used enums will have preprocessor macros which give
 * a shorter, more convenient naming pattern. If this isn't desired, simply
 * comment the following line out.
 *
 * For instance, E_CONTROLLER_MASTER has a shorter name: CONTROLLER_MASTER.
 * E_CONTROLLER_MASTER is pedantically correct within the PROS styleguide, but
 * not convienent for most student programmers.
 */
#define PROS_USE_SIMPLE_NAMES

/**
 * If defined, C++ literals will be available for use. All literals are in the
 * pros::literals namespace.
 *
 * For instance, you can do 4_mtr = 50 to set motor 4's target velocity to 50
 */
#define PROS_USE_LITERALS

#include "api.h"
#include <bits/stdc++.h>//Added by Sukanu

/**
 * You should add more #includes here
 */
//#include "okapi/api.hpp"
//#include "pros/api_legacy.h"

/**
 * If you find doing pros::Motor() to be tedious and you'd prefer just to do
 * Motor, you can use the namespace with the following commented out line.
 *
 * IMPORTANT: Only the okapi or pros namespace may be used, not both
 * concurrently! The okapi namespace will export all symbols inside the pros
 * namespace.
 */
using namespace pros;
using namespace std;//Added by Sukanu
// using namespace pros::literals;
// using namespace okapi;

/**
 * Prototypes for the competition control tasks are redefined here to ensure
 * that they can be called from user code (i.e. calling autonomous from a
 * button press in opcontrol() for testing purposes).
 */
#ifdef __cplusplus
extern "C" {
#endif
void autonomous(void);
void initialize(void);
void disabled(void);
void competition_initialize(void);
void opcontrol(void);
#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
/**
 * You can add C++-only headers here
 */
#include <iostream>
#endif

#endif  // _PROS_MAIN_H_

 

来自台州, 浙江, 中国