Komut Tabanlı Bir Robot Projesi Yapılandırma
Kullanıcılar komut tabanlı kütüphaneleri istedikleri gibi kullanmakta özgür olduklarında (ki ileri düzey kullanıcılar bunu yapmaya teşvik edilir),yeni kullanıcılar temel komut tabanlı bir robotun nasıl yapılandırılacağı konusunda yardım isteyebilir.
A standard template for a command-based robot project is included in the WPILib examples repository (Java, C++). This section will walk users through the structure of this template.
Root paket /dizin genellikle 4 sınıf içerir:
‘’Main’’, robotun ana uygulamasıdır (yalnızca Java).Yeni kullanıcılar kesinlikle bu sınıfa dokunmamalıdır. ‘’Robot’’, robotun kodunun ana kontrol akışından sorumludur. RobotContainer, robot alt sistemlerini ve komutlarını tutar, ve açıklayıcı robot kurulumunun çoğunun (örneğin düğme bağlamaları) yapıldığı yerdir.``Constants``, robotun tamamında kullanılacak olan ulusal erişilebilir sabitler tutar.
root dizini ayrıca iki alt-paket / alt-dizin içerir: `` Subsystems ‘’, tüm kullanıcı tanımlı alt sistem sınıflarını içerir. `` Commands ‘’, tüm kullanıcı tanımlı komut sınıflarını içerir.
Robot
As Robot (Java, C++ (Header), C++ (Source)) is responsible for the program’s control flow, and command-based is an declarative paradigm designed to minimize the amount of attention the user has to pay to explicit program control flow, the Robot class of a command-based project should be mostly empty. However, there are a few important things that must be included
21 /**
22 * This function is run when the robot is first started up and should be used for any
23 * initialization code.
24 */
25 public Robot() {
26 // Instantiate our RobotContainer. This will perform all our button bindings, and put our
27 // autonomous chooser on the dashboard.
28 m_robotContainer = new RobotContainer();
29 }
In Java, an instance of RobotContainer should be constructed during the Robot constructor - this is important, as most of the declarative robot setup will be called from the RobotContainer constructor.
C++ da,RobotContainer bir değer üyesi olduğuiçin ve `` Robot’’un yapımı sırasında yapılacağı için buna gerek yoktur.
31 /**
32 * This function is called every 20 ms, no matter the mode. Use this for items like diagnostics
33 * that you want ran during disabled, autonomous, teleoperated and test.
34 *
35 * <p>This runs after the mode specific periodic functions, but before LiveWindow and
36 * SmartDashboard integrated updating.
37 */
38 @Override
39 public void robotPeriodic() {
40 // Runs the Scheduler. This is responsible for polling buttons, adding newly-scheduled
41 // commands, running already-scheduled commands, removing finished or interrupted commands,
42 // and running subsystem periodic() methods. This must be called from the robot's periodic
43 // block in order for anything in the Command-based framework to work.
44 CommandScheduler.getInstance().run();
45 }
11/**
12 * This function is called every 20 ms, no matter the mode. Use
13 * this for items like diagnostics that you want to run during disabled,
14 * autonomous, teleoperated and test.
15 *
16 * <p> This runs after the mode specific periodic functions, but before
17 * LiveWindow and SmartDashboard integrated updating.
18 */
19void Robot::RobotPeriodic() {
20 frc2::CommandScheduler::GetInstance().Run();
21}
`` RobotPeriodic () ‘’ yöntemine `` CommandScheduler.getInstance (). Run () ‘’ çağrısının dahil edilmesi önemlidir; bu çağrı olmadan, zamanlayıcı programlanmış herhangi bir komutu yürütmeyecektir. `` TimedRobot ‘’ varsayılan ana döngü frekansı 50Hz ile çalıştığı için bu, periyodik komut ve alt sistem yöntemlerinin çağrılacağı frekanstır. Yeni kullanıcıların bu yöntemi kodlarının herhangi bir yerinden çağırmaları önerilmez.
54 /** This autonomous runs the autonomous command selected by your {@link RobotContainer} class. */
55 @Override
56 public void autonomousInit() {
57 m_autonomousCommand = m_robotContainer.getAutonomousCommand();
58
59 // schedule the autonomous command (example)
60 if (m_autonomousCommand != null) {
61 CommandScheduler.getInstance().schedule(m_autonomousCommand);
62 }
63 }
32/**
33 * This autonomous runs the autonomous command selected by your {@link
34 * RobotContainer} class.
35 */
36void Robot::AutonomousInit() {
37 m_autonomousCommand = m_container.GetAutonomousCommand();
38
39 if (m_autonomousCommand) {
40 frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand.value());
41 }
42}
autonomousInit()" yöntemi, ``RobotContainer örneği tarafından döndürülen bir komutu zamanlar. Hangi otonom komutun çalıştırılacağını seçme mantığı, `` RobotContainer ‘’ içinde kullanılabilir.
69 @Override
70 public void teleopInit() {
71 // This makes sure that the autonomous stops running when
72 // teleop starts running. If you want the autonomous to
73 // continue until interrupted by another command, remove
74 // this line or comment it out.
75 if (m_autonomousCommand != null) {
76 m_autonomousCommand.cancel();
77 }
78 }
46void Robot::TeleopInit() {
47 // This makes sure that the autonomous stops running when
48 // teleop starts running. If you want the autonomous to
49 // continue until interrupted by another command, remove
50 // this line or comment it out.
51 if (m_autonomousCommand) {
52 m_autonomousCommand->Cancel();
53 }
54}
teleopInit()` yöntemi her çalışan otonom komutu reddeder.Bu genellikle iyi bir uygulamadır.
İleri düzey kullanıcılar, uygun gördükleri şekilde çeşitli başlangıç ve periyodik yöntemlere ek kod eklemekte özgürdürler; fakat, `` Robot.java ‘’ ya büyük miktarlarda zorunlu robot kodu dahil etmenin, komut tabanlı paradigmanın açıklayıcı tasarım felsefesine aykırı olduğunu ve kafa karıştırıcı bir şekilde yapılandırılmış / düzensiz kodla sonuçlanabileceği unutulmamalıdır.
RobotContainer
This class (Java, C++ (Header), C++ (Source)) is where most of the setup for your command-based robot will take place. In this class, you will define your robot’s subsystems and commands, bind those commands to triggering events (such as buttons), and specify which command you will run in your autonomous routine. There are a few aspects of this class new users may want explanations for:
23 private final ExampleSubsystem m_exampleSubsystem = new ExampleSubsystem();
32 ExampleSubsystem m_subsystem;
Dikkat et Alt-sistemler gizli olarak RobotContainer içine atanmıştır. Bu, komuta dayalı yapının önceki enkarnasyonuyla büyük bir zıtlık içindedir, ancak üzerinde mutabık kalınan nesne odaklı uygulamalarla çok daha uyumludur. Eğer alt sistemler ulusal değişkenler olarak atanırsa, kullanıcılara kodun istedikleri yerinden bunlara ulaşmalarına izin verir. Bu, bazı şeyleri kolaylaştırabilirken (örneğin, bu komutlara erişmeleri için alt sistemleri komutlara geçirmeye gerek kalmaz), programın kontrol akışını olmadığı için takip etmeyi çok daha zor hale getirir. Kodun hangi bölümlerinin, kodun diğer bölümleri tarafından değiştirilebileceği veya değiştirildiği hemen anlaşılır. Erişim kolaylığı, kullanıcıların kaynak tarafından yönetilen komutların dışında yanlışlıkla alt sistem yöntemlerine çakışan çağrılar yapmasını kolaylaştırdığından, bu aynı zamanda kaynak yönetimi sisteminin işini yapmasına da engel olur.
61 return Autos.exampleAuto(m_exampleSubsystem);
34 return autos::ExampleAuto(&m_subsystem);
Alt-sistemler gizli üyeler olarak açıklandığından beri,bu komutların komutları çağırması için komutlara açıkça aktarılmaları gerekir(“dependency injection” adı verilen bir kalıp). Bu, burada bir `` ExampleSubsystem ‘’ e bir işaretçi iletilen `` ExampleCommand ‘’ ile yapılır.
35 /**
36 * Use this method to define your trigger->command mappings. Triggers can be created via the
37 * {@link Trigger#Trigger(java.util.function.BooleanSupplier)} constructor with an arbitrary
38 * predicate, or via the named factories in {@link
39 * edu.wpi.first.wpilibj2.command.button.CommandGenericHID}'s subclasses for {@link
40 * CommandXboxController Xbox}/{@link edu.wpi.first.wpilibj2.command.button.CommandPS4Controller
41 * PS4} controllers or {@link edu.wpi.first.wpilibj2.command.button.CommandJoystick Flight
42 * joysticks}.
43 */
44 private void configureBindings() {
45 // Schedule `ExampleCommand` when `exampleCondition` changes to `true`
46 new Trigger(m_exampleSubsystem::exampleCondition)
47 .onTrue(new ExampleCommand(m_exampleSubsystem));
48
49 // Schedule `exampleMethodCommand` when the Xbox controller's B button is pressed,
50 // cancelling on release.
51 m_driverController.b().whileTrue(m_exampleSubsystem.exampleMethodCommand());
52 }
19void RobotContainer::ConfigureBindings() {
20 // Configure your trigger bindings here
21
22 // Schedule `ExampleCommand` when `exampleCondition` changes to `true`
23 frc2::Trigger([this] {
24 return m_subsystem.ExampleCondition();
25 }).OnTrue(ExampleCommand(&m_subsystem).ToPtr());
26
27 // Schedule `ExampleMethodCommand` when the Xbox controller's B button is
28 // pressed, cancelling on release.
29 m_driverController.B().WhileTrue(m_subsystem.ExampleMethodCommand());
30}
As mentioned before, the RobotContainer() constructor is where most of the declarative setup for the robot should take place, including button bindings, configuring autonomous selectors, etc. If the constructor gets too “busy,” users are encouraged to migrate code into separate subroutines (such as the configureBindings() method included by default) which are called from the constructor.
54 /**
55 * Use this to pass the autonomous command to the main {@link Robot} class.
56 *
57 * @return the command to run in autonomous
58 */
59 public Command getAutonomousCommand() {
60 // An example command will be run in autonomous
61 return Autos.exampleAuto(m_exampleSubsystem);
62 }
63}
32frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
33 // An example command will be run in autonomous
34 return autos::ExampleAuto(&m_subsystem);
35}
Son olarak, `` getAutonomousCommand() ‘’ yöntemi, kullanıcıların seçtikleri otonom komutlarını ana Robot sınıfına göndermeleri için uygun bir yol sağlar (otonom başladığında onu programlamak için ona erişim gerekir).
Sabitler
The Constants class (Java, C++ (Header)) (in C++ this is not a class, but simply a header file in which several namespaces are defined) is where globally-accessible robot constants (such as speeds, unit conversion factors, PID gains, and sensor/motor ports) can be stored. It is recommended that users separate these constants into individual inner classes corresponding to subsystems or robot modes, to keep variable names shorter.
Javada, tüm sabitler public static final olarak açıklanmalıdır ki ulusal olarak erişilebilsin ve değiştirilemesin. C++ ‘da tüm sabitler constexpr olmalıdır.
Bir `` constants ‘’ sınıfının pratikte nasıl görünmesi gerektiğine dair daha fazla açıklayıcı örnekler için, çeşitli komut tabanlı örnek projelerin örneklerine bakın:
Javada, sabitlerin diğer sınıflardan gerekli iç- sınıfı içe aktararak kullanılması önerilir. Bir import static ifadesi bir sınıfın statik ad alanını içinde çalıştığınız sınıfa aktarır, yani her static sabiti eğer sınıfın içinde tanımlıysa direkt başvurulanabilir. C++ ‘da, aynı etki using namespace: kullanılınarak elde edilebilir.
import static edu.wpi.first.wpilibj.templates.commandbased.Constants.OIConstants.*;
using namespace OIConstants;
Alt Sistemler
Kullanıcı tanımlı alt sistemler bu paket / dizinine girmelidir.
Komutlar
Kullanıcı tanımlı komutlar bu paket / dizinine girmelidir.