WPILibC++ 2023.4.3-108-ge5452e3
Mechanism2d.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <memory>
8#include <string>
9
14#include <wpi/StringMap.h>
15#include <wpi/mutex.h>
17
19#include "frc/util/Color8Bit.h"
20
21namespace frc {
22
23/**
24 * Visual 2D representation of arms, elevators, and general mechanisms through
25 * a node-based API.
26 *
27 * A Mechanism2d object is published and contains at least one root node. A root
28 * is the anchor point of other nodes (such as ligaments). Other nodes are
29 * recursively appended based on other nodes.
30 *
31 * Except for the Mechanism2d container object, none of the objects should be
32 * passed or interacted with by value! Obtain pointers from factory methods such
33 * as Mechanism2d.GetRoot() and MechanismObject2d.Append<>(). The Mechanism2d
34 * container object owns the root nodes, and each node internally owns the nodes
35 * based on it. Beware not to let the Mechanism2d object out of scope - all
36 * nodes will be recursively destructed!
37 *
38 * @see MechanismObject2d
39 * @see MechanismLigament2d
40 * @see MechanismRoot2d
41 */
43 public wpi::SendableHelper<Mechanism2d> {
44 public:
45 /**
46 * Create a new Mechanism2d with the given dimensions and background color.
47 *
48 * The dimensions represent the canvas that all the nodes are drawn on. The
49 * default color is dark blue.
50 *
51 * @param width the width
52 * @param height the height
53 * @param backgroundColor the background color
54 */
55 Mechanism2d(double width, double height,
56 const Color8Bit& backgroundColor = {0, 0, 32});
57
58 /**
59 * Get or create a root in this Mechanism2d with the given name and
60 * position.
61 *
62 * <p>If a root with the given name already exists, the given x and y
63 * coordinates are not used.
64 *
65 * @param name the root name
66 * @param x the root x coordinate
67 * @param y the root y coordinate
68 * @return a new root object, or the existing one with the given name.
69 */
70 MechanismRoot2d* GetRoot(std::string_view name, double x, double y);
71
72 /**
73 * Set the Mechanism2d background color.
74 *
75 * @param color the new background color
76 */
78
79 void InitSendable(nt::NTSendableBuilder& builder) override;
80
81 private:
82 double m_width;
83 double m_height;
84 std::string m_color;
85 mutable wpi::mutex m_mutex;
86 std::shared_ptr<nt::NetworkTable> m_table;
89 nt::StringPublisher m_colorPub;
90};
91} // namespace frc
This file defines the StringMap class.
Represents colors that can be used with Addressable LEDs.
Definition: Color8Bit.h:17
Visual 2D representation of arms, elevators, and general mechanisms through a node-based API.
Definition: Mechanism2d.h:43
void SetBackgroundColor(const Color8Bit &color)
Set the Mechanism2d background color.
void InitSendable(nt::NTSendableBuilder &builder) override
Initializes this Sendable object.
MechanismRoot2d * GetRoot(std::string_view name, double x, double y)
Get or create a root in this Mechanism2d with the given name and position.
Mechanism2d(double width, double height, const Color8Bit &backgroundColor={0, 0, 32})
Create a new Mechanism2d with the given dimensions and background color.
Root Mechanism2d node.
Definition: MechanismRoot2d.h:26
NetworkTables DoubleArray publisher.
Definition: DoubleArrayTopic.h:157
Definition: NTSendableBuilder.h:18
Interface for NetworkTable Sendable objects.
Definition: NTSendable.h:16
NetworkTables String publisher.
Definition: StringTopic.h:159
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:19
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:114
basic_string_view< char > string_view
Definition: core.h:520
const Scalar & y
Definition: MathFunctions.h:821
Definition: AprilTagPoseEstimator.h:15
::std::mutex mutex
Definition: mutex.h:17
color
Definition: color.h:16