ArdEV3
Use EV3 peripherals from Arduino microcontrollers.
Loading...
Searching...
No Matches
EV3Motor.h
1#pragma once
2#include <stdint.h>
3#include <EV3Peripheral.h>
4
8
12
16
17
18#define ev3Motor_EncoderGlobal(motor) void ev3Motor_STATIC_##motor##__EncoderWrapper() {motor._CountPulse();}
19
20#define ev3Motor_SetupEncoder(motor) attachInterrupt(digitalPinToInterrupt(motor.Ports().EncoderA), ev3Motor_STATIC_##motor##__EncoderWrapper, RISING);
21
22namespace ev3 {
23
43 uint8_t Enable, In1, In2, EncoderA, EncoderB;
44};
45
46enum MotorDirection {
47 FORWARD, REVERSE
48};
49
60class Motor : public _::Peripheral<MotorPortConfig> {
61 MotorPortConfig ports;
62 volatile unsigned long encoder = 0;
63 bool encoding = false;
64 bool runningToPosition = false;
65 unsigned long target = 0;
66
67 void checkDistance();
68public:
69 EV3_PERIPHERAL_CONSTRUCT(Motor)
70
71 void SetPower(uint8_t power);
72 void SetDirection(MotorDirection direction);
73 void StartEncoder();
74 void StopEncoder();
75 void ResetEncoder();
76 void RunToPosition(uint8_t power, unsigned long position);
77
78 void _CountPulse();
79
80 virtual void Setup();
81 virtual void Loop();
82};
83}
Definition EV3Motor.h:60
void ResetEncoder()
Reset the encoder to zero.
Definition EV3Motor.cc:20
void RunToPosition(uint8_t power, unsigned long position)
Set the motor running to the specified position at the specified power. This will complete asynchrono...
Definition EV3Motor.cc:33
virtual void Loop()
Run once per cycle – call this in your global loop function.
Definition EV3Motor.cc:61
virtual void Setup()
Initialize the motor. Call this in your global setup function.
Definition EV3Motor.cc:48
void StartEncoder()
Start tracking the encoder position.
Definition EV3Motor.cc:12
void SetPower(uint8_t power)
Set the power of the motor. This writes a PWM signal to the enable pin.
Definition EV3Motor.cc:24
void SetDirection(MotorDirection direction)
Set the direction of the motor. This writes digital signals to the input 1 and 2 pins.
Definition EV3Motor.cc:28
void StopEncoder()
Stop tracking the encoder position.
Definition EV3Motor.cc:16
Definition EV3Peripheral.h:9
Definition EV3Motor.h:42