SPIKE-RT C++ API Reference
Loading...
Searching...
No Matches
Motor.h
1//
2// Motor.h
3//
4// Copyright (c) 2025 Embedded Technology Software Design Robot Contest
5//
6
7#ifndef SPIKE_CPP_API_MOTOR_H_
8#define SPIKE_CPP_API_MOTOR_H_
9
10#include <stdint.h>
11extern "C" {
12#include <pup/motor.h>
13}
14
15#include "Port.h"
16
17namespace spikeapi {
21class Motor
22{
23public:
24
25 enum class EDirection {
26 CLOCKWISE = PUP_DIRECTION_CLOCKWISE,
27 COUNTERCLOCKWISE = PUP_DIRECTION_COUNTERCLOCKWISE,
28 };
29
37 Motor(EPort port,EDirection direction = EDirection::CLOCKWISE, bool reset_count = true) : mHasError(false) {
38 mMotor = pup_motor_get_device(static_cast<pbio_port_id_t>(port));
39 if ( !mMotor ) {
40 mHasError = true;
41 return;
42 }
43 pbio_error_t ret = pup_motor_setup(mMotor, static_cast<pup_direction_t>(direction), reset_count);
44 if ( ret != PBIO_SUCCESS ) {
45 mHasError = true;
46 }
47 }
48
53 void resetCount() const {
54 pup_motor_reset_count(mMotor);
55 }
56
61 int32_t getCount() const {
62 return pup_motor_get_count(mMotor);
63 }
64
69 int32_t getSpeed() const {
70 return pup_motor_get_speed(mMotor);
71 }
72
78 void setSpeed(int speed) const {
79 pup_motor_set_speed(mMotor, speed);
80 }
81
86 int32_t getPower() const {
87 return pup_motor_get_power(mMotor);
88 }
89
95 void setPower(int power) const {
96 pup_motor_set_power(mMotor, power);
97 }
98
103 void stop() const {
104 pup_motor_stop(mMotor);
105 }
106
111 void brake() const {
112 pup_motor_brake(mMotor);
113 }
114
119 void hold() const {
120 pup_motor_hold(mMotor);
121 }
122
128 bool isStalled() const {
129 return pup_motor_is_stalled(mMotor);
130 }
131
137 int32_t setDutyLimit(int duty_limit) const {
138 return pup_motor_set_duty_limit(mMotor, duty_limit);
139 }
140
145 void restoreDutyLimit(int old_value) const {
146 pup_motor_restore_duty_limit(mMotor, old_value);
147 }
148
152 bool hasError() { return mHasError; }
153
154
155private:
156 pup_motor_t *mMotor;
157 bool mHasError;
158
159}; // class Motor
160} // namespace spikeapi
161
162#endif // !SPIKE_CPP_API_MOTOR_H_
void stop() const
Definition Motor.h:103
void restoreDutyLimit(int old_value) const
Definition Motor.h:145
void brake() const
Definition Motor.h:111
void setPower(int power) const
Definition Motor.h:95
Motor(EPort port, EDirection direction=EDirection::CLOCKWISE, bool reset_count=true)
Definition Motor.h:37
bool isStalled() const
Definition Motor.h:128
int32_t getCount() const
Definition Motor.h:61
bool hasError()
Definition Motor.h:152
int32_t setDutyLimit(int duty_limit) const
Definition Motor.h:137
void resetCount() const
Definition Motor.h:53
void setSpeed(int speed) const
Definition Motor.h:78
int32_t getPower() const
Definition Motor.h:86
void hold() const
Definition Motor.h:119
int32_t getSpeed() const
Definition Motor.h:69