1
+ #include " config.h"
2
+
3
+ #if USE_GPIO == 1
4
+
5
+ #include " DCMotor.h"
6
+ #include < iostream>
7
+ #include < spdlog/spdlog.h>
8
+ #include < utility>
9
+ #include < wiringSerial.h>
10
+
11
+ DCMotorOutput::DCMotorOutput (std::string name, int pinForward, int pinReverse, int motorPower, int limitSwitchMinPin,
12
+ int limitSwitchMaxPin, int potentiometerPin)
13
+ : name(std::move(name)), pinNbrForward(pinForward), pinNbrReverse(pinReverse), motorPower(motorPower),
14
+ limitSwitchMinPin(limitSwitchMinPin), limitSwitchMaxPin(limitSwitchMaxPin), potentiometerPin(potentiometerPin)
15
+ {
16
+ logger = spdlog::default_logger ();
17
+
18
+ SPDLOG_LOGGER_DEBUG (logger, " Created PwmOutput {}" , name);
19
+
20
+ #if USE_ARDUINO_PROXY == 1
21
+ arduinoProxy = ArduinoProxy::getInstance ();
22
+
23
+ RocketryProto::ArduinoIn arduinoIn;
24
+ arduinoIn.mutable_dcmotorinit ()->set_motorforwardpin (pinForward);
25
+ arduinoIn.mutable_dcmotorinit ()->set_motorreversepin (pinReverse);
26
+ arduinoIn.mutable_dcmotorinit ()->set_motorpower (motorPower);
27
+ arduinoIn.mutable_dcmotorinit ()->set_limitswitchminpin (limitSwitchMinPin);
28
+ arduinoIn.mutable_dcmotorinit ()->set_limitswitchmaxpin (limitSwitchMaxPin);
29
+ arduinoIn.mutable_dcmotorinit ()->set_potentiometerpin (potentiometerPin);
30
+ arduinoProxy->send (arduinoIn);
31
+ #endif
32
+ }
33
+
34
+ bool DCMotorOutput::setValue (int value)
35
+ {
36
+ if (currentState != value)
37
+ {
38
+ currentState = value;
39
+ SPDLOG_LOGGER_INFO (logger, " DC Motor {} changed to {} on pin {} and {}" , name, currentState, pinNbrForward,
40
+ pinNbrReverse);
41
+
42
+ #if USE_ARDUINO_PROXY == 1
43
+ RocketryProto::ArduinoIn arduinoIn;
44
+ arduinoIn.mutable_dcmotorcontrol ()->set_pinforward (pinNbrForward);
45
+ arduinoIn.mutable_dcmotorcontrol ()->set_pinreverse (pinNbrReverse);
46
+ arduinoIn.mutable_dcmotorcontrol ()->set_position (value);
47
+
48
+ arduinoProxy->send (arduinoIn);
49
+ #endif
50
+ }
51
+ return true ;
52
+ }
53
+
54
+ DCMotorState DCMotorOutput::getCurrentState ()
55
+ {
56
+ #if USE_ARDUINO_PROXY == 1
57
+ try
58
+ {
59
+ return arduinoProxy->getDCMotorState (pinNbrForward, pinNbrReverse);
60
+ }
61
+ catch (std::out_of_range &error)
62
+ {
63
+ return {-1 , 0 , false , false , std::chrono::steady_clock::now ()};
64
+ }
65
+ #else
66
+ return {-1 , 0 , false , false , std::chrono::steady_clock::now ()};
67
+ #endif
68
+ }
69
+
70
+ #endif
0 commit comments