| EVShield v1.3 EVShield Library Reference by mindsensors.com

EVShield.h
Go to the documentation of this file.
1
39 /*
40 * EVShield interface library
41 * Copyright (C) 2016 mindsensors.com
42 * 12/18/2014 Nitin Patil -- modified to work with EVshield
43 * Feb 2017 Seth Tenembaum -- modified to work with PiStorms, added touchscreen defines and methods
44 *
45 * This file is part of EVShield interface library.
46 * This library is free software; you can redistribute it and/or
47 * modify it under the terms of the GNU Lesser General Public
48 * License as published by the Free Software Foundation; either
49 * version 2.1 of the License, or (at your option) any later version.
50
51 * This library is distributed in the hope that it will be useful,
52 * but WITHOUT ANY WARRANTY; without even the implied warranty of
53 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
54 * Lesser General Public License for more details.
55
56 * You should have received a copy of the GNU Lesser General Public
57 * License along with this library; if not, write to the Free Software
58 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
59 */
60
61 #ifndef EVShield_H
62 #define EVShield_H
63
64 #include "SHDefines.h"
65
66 // Motor control related constants.
67 #define SH_CONTROL_SPEED 0x01
68 #define SH_CONTROL_RAMP 0x02
69 #define SH_CONTROL_RELATIVE 0x04
70 #define SH_CONTROL_TACHO 0x08
71 #define SH_CONTROL_BRK 0x10
72 #define SH_CONTROL_ON 0x20
73 #define SH_CONTROL_TIME 0x40
74 #define SH_CONTROL_GO 0x80
75
76 #define SH_STATUS_SPEED 0x01
77 #define SH_STATUS_RAMP 0x02
78 #define SH_STATUS_MOVING 0x04
79 #define SH_STATUS_TACHO 0x08
80 #define SH_STATUS_BREAK 0x10
81 #define SH_STATUS_OVERLOAD 0x20
82 #define SH_STATUS_TIME 0x40
83 #define SH_STATUS_STALL 0x80
84
85 #define SH_COMMAND 0x41
86 #define SH_VOLTAGE 0x6E
87
88 #define SH_SETPT_M1 0x42
89 #define SH_SPEED_M1 0x46
90 #define SH_TIME_M1 0x47
91 #define SH_CMD_B_M1 0x48
92 #define SH_CMD_A_M1 0x49
93
94 #define SH_SETPT_M2 0x4A
95 #define SH_SPEED_M2 0x4E
96 #define SH_TIME_M2 0x4F
97 #define SH_CMD_B_M2 0x50
98 #define SH_CMD_A_M2 0x51
99
100 /*
101 * Read registers.
102 */
103 #define SH_POSITION_M1 0x52
104 #define SH_POSITION_M2 0x56
105 #define SH_STATUS_M1 0x5A
106 #define SH_STATUS_M2 0x5B
107 #define SH_TASKS_M1 0x5C
108 #define SH_TASKS_M2 0x5D
109
110 #define SH_ENCODER_PID 0x5E
111 #define SH_SPEED_PID 0x64
112 #define SH_PASS_COUNT 0x6A
113 #define SH_TOLERANCE 0x6B
114
115 #define SH_S1_MODE 0x6F
116 #define SH_S1_EV3_MODE 0x6F
117 #define SH_S1_ANALOG 0x70
118
119 #define SH_S2_MODE 0xA3
120 #define SH_S2_EV3_MODE 0x6F
121 #define SH_S2_ANALOG 0xA4
122
123 #define SH_BTN_PRESS 0xDA
124 #define SH_RGB_LED 0xD7
125 #define SH_CENTER_RGB_LED 0xDE
126
127 #define SH_PS_TS_X 0xE3
128 #define SH_PS_TS_Y 0xE5
129 #define SH_PS_TS_RAWX 0xE7
130 #define SH_PS_TS_RAWY 0xE9
131 #define SH_PS_TS_CALIBRATION_DATA_READY 0x70
132 #define SH_PS_TS_CALIBRATION_DATA 0x71
133 #define SH_PS_TS_SAVE 0x77
134 #define SH_PS_TS_LOAD 0x6C
135 #define SH_PS_TS_UNLOCK 0x45
136
137 /* constants to be used by user programs */
141 typedef enum {
142 SH_Motor_1 = 0x01,
143 SH_Motor_2 = 0x02,
144 SH_Motor_Both = 0x03
145 } SH_Motor;
146
147 /*
148 * \enum SH_Next_Action Next action related constants
149 */
150 typedef enum {
151 SH_Next_Action_Float = 0x00,
152 SH_Next_Action_Brake = 0x01,
153 SH_Next_Action_BrakeHold = 0x02
154 } SH_Next_Action;
155
159 typedef enum {
160 SH_Direction_Reverse = 0x00,
161 SH_Direction_Forward = 0x01
162 } SH_Direction;
163
164 /*
165 * \enum SH_Move Tachometer movement related constants
166 */
167 typedef enum {
168 SH_Move_Absolute = 0x00,
169 SH_Move_Relative = 0x01
170 } SH_Move;
171
172 /*
173 * \enum SH_Completion_Wait Whether to wait for motor to finish it's current task or not
174 */
175 typedef enum {
176 SH_Completion_Dont_Wait = 0x00,
177 SH_Completion_Wait_For = 0x01
178 } SH_Completion_Wait;
179
180 /*
181 * Speed constants, these are just convenience constants,
182 * You can use any value between 0 and 100.
183 */
184 #define SH_Speed_Full 90
185 #define SH_Speed_Medium 60
186 #define SH_Speed_Slow 25
187
188 /*
189 * EVShield has two banks, and each of them has different I2C address.
190 * each bank has 2 motors and 2 sensors.
191 *
192 */
197 #define SH_Bank_A 0x34
198
202 #define SH_Bank_B 0x36
203
204
205 /*
206 * Sensor type primitives
207 *
208 */
209
214 #define SH_Type_NONE 0x00
215
216 #define SH_Type_SWITCH 0x01
217
222 #define SH_Type_ANALOG 0x02
223
224
229 #define SH_Type_LIGHT_REFLECTED 0x03
230
234 #define SH_Type_LIGHT_AMBIENT 0x04
235
239 #define SH_Type_I2C 0x09
240
245 #define SH_Type_COLORFULL 13
246
250 #define SH_Type_COLORRED 14
251
255 #define SH_Type_COLORGREEN 15
256
260 #define SH_Type_COLORBLUE 16
261
265 #define SH_Type_COLORNONE 17
266
270 #define SH_Type_EV3_SWITCH 18
271
275 #define SH_Type_EV3 19
276
277
278
279 /*
280 * Sensor defines.
281 */
288 #define SH_S1 1
289
295 #define SH_S2 2
296
297 #include "EVShieldI2C.h"
298 #if defined(__AVR__)
299 #include <avr/io.h>
300 #include <avr/interrupt.h>
301 #endif
302
306 class EVShieldBank : public EVShieldI2C
307 {
308 public:
310 EVShieldBank(uint8_t i2c_address = SH_Bank_A);
311
319 int evshieldGetBatteryVoltage();
320
323 int nxshieldGetBatteryVoltage();
324
329 uint8_t EVShieldIssueCommand(char command);
330
331 //
332 // Motor Operation APIs.
333 //
338 bool motorSetEncoderTarget(SH_Motor which_motor, long target);
339
345 long motorGetEncoderTarget(SH_Motor which_motor);
346
352 bool motorSetSpeed(SH_Motor which_motor, int speed);
353
359 int8_t motorGetSpeed(SH_Motor which_motor);
360
366 bool motorSetTimeToRun(SH_Motor which_motor, int seconds);
367
373 uint8_t motorGetTimeToRun(SH_Motor which_motor);
374
385 bool motorSetCommandRegB(SH_Motor which_motor, uint8_t value);
394 uint8_t motorGetCommandRegB(SH_Motor which_motor);
404 bool motorSetCommandRegA(SH_Motor which_motor, uint8_t value);
413 uint8_t motorGetCommandRegA(SH_Motor which_motor);
414
420 int32_t motorGetEncoderPosition(SH_Motor which_motor);
421
429 uint8_t motorGetStatusByte(SH_Motor which_motor);
430
437 uint8_t motorGetTasksRunningByte(SH_Motor which_motor);
438
446 bool motorSetEncoderPID(uint16_t Kp, uint16_t Ki, uint16_t Kd);
447
455 bool motorSetSpeedPID(uint16_t Kp, uint16_t Ki, uint16_t Kd);
456
457 bool centerLedSetRGB(uint8_t R, uint8_t G, uint8_t B);
458
459 // Set the RGBLED that shows RGB color
460
461 bool ledSetRGB(uint8_t R, uint8_t G, uint8_t B);
462
470 bool motorSetPassCount(uint8_t pass_count);
471
478 bool motorSetTolerance(uint8_t tolerance);
479
484 bool motorReset();
485
490 bool motorStartBothInSync();
491
496 bool motorResetEncoder(SH_Motor which_motor);
497
508 bool motorSetSpeedTimeAndControl(SH_Motor which_motors, int speed,
509 uint8_t duration, uint8_t control);
510
523 bool motorSetEncoderSpeedTimeAndControl(SH_Motor which_motors,
524 long encoder, int speed,
525 uint8_t duration, uint8_t control);
526
533 uint8_t motorIsTimeDone(SH_Motor which_motors);
534
541 uint8_t motorWaitUntilTimeDone(SH_Motor which_motors);
542
549 uint8_t motorIsTachoDone(SH_Motor which_motors);
550
557 uint8_t motorWaitUntilTachoDone(SH_Motor which_motors);
558
566 void motorRunUnlimited(SH_Motor which_motors, SH_Direction direction,
567 int speed);
568
579 uint8_t motorRunSeconds(SH_Motor which_motors, SH_Direction direction,
580 int speed, uint8_t duration,
581 SH_Completion_Wait wait_for_completion,
582 SH_Next_Action next_action);
583
596 uint8_t motorRunTachometer(SH_Motor which_motors, SH_Direction direction,
597 int speed, long tachometer,
598 SH_Move relative,
599 SH_Completion_Wait wait_for_completion,
600 SH_Next_Action next_action);
601
613 uint8_t motorRunDegrees(SH_Motor which_motors, SH_Direction direction,
614 int speed, long degrees,
615 SH_Completion_Wait wait_for_completion,
616 SH_Next_Action next_action);
617
629 uint8_t motorRunRotations(SH_Motor which_motors, SH_Direction direction,
630 int speed, long rotations,
631 SH_Completion_Wait wait_for_completion,
632 SH_Next_Action next_action);
633
639 bool motorStop(SH_Motor which_motors, SH_Next_Action next_action);
640
641
642 //
643 // EVShield sensor functions.
644 //
645 public:
652 bool sensorSetType(uint8_t which_sensor, uint8_t sensor_type);
653
659 int sensorReadRaw(uint8_t which_sensor);
660
661 };
662
667 class EVShieldBankB : public EVShieldBank
668 {
669 private:
670
671 public:
673 EVShieldBankB(uint8_t i2c_address_b = SH_Bank_B);
679 int sensorReadRaw(uint8_t which_sensor);
680
687 bool sensorSetType(uint8_t which_sensor, uint8_t sensor_type);
688 };
689
690
694 class EVShield
695 {
696 public:
700 uint8_t m_protocol;
703 EVShieldBank bank_a;
706 EVShieldBankB bank_b;
707
709 EVShield(uint8_t i2c_address_a = SH_Bank_A,
710 uint8_t i2c_address_b = SH_Bank_B);
711
717 void init(SH_Protocols protocol=SH_HardwareI2C);
718
722 void initLEDTimers();
723
727 void I2CTimer();
728
733 void initProtocols(SH_Protocols protocol=SH_HardwareI2C);
734
735
745 bool getButtonState(uint8_t btn);
746
754 void waitForButtonPress(uint8_t btn, uint8_t led_pattern=1);
755
756
764 void ledSetRGB(uint8_t red = 0, uint8_t green = 0, uint8_t blue = 0);
765
770 void ledBreathingPattern();
771
776 void ledHeartBeatPattern();
777
784 void getTouchscreenValues(uint16_t *x, uint16_t *y);
785
790 uint16_t TS_X();
791
796 uint16_t TS_Y();
797
802 bool isTouched();
803
808 bool checkButton(uint16_t x, uint16_t y, uint16_t width, uint16_t height);
809
814 uint8_t getFunctionButton();
815
816 private:
818 uint16_t x1, y1, x2, y2, x3, y3, x4, y4;
819
821 uint16_t RAW_X();
822
824 uint16_t RAW_Y();
825
826 bool useOldTouchscreen = false;
827
832 void getReading(uint16_t *x, uint16_t *y);
833 };
834
839 extern bool format_bin(uint8_t i, char *s);
840
841 #endif
SH_Next_Action_Float
Definition: EVShield.h:151
EVShieldBank::motorGetSpeed
int8_t motorGetSpeed(SH_Motor which_motor)
Definition: EVShield.cpp:264
EVShieldBank::motorSetEncoderSpeedTimeAndControl
bool motorSetEncoderSpeedTimeAndControl(SH_Motor which_motors, long encoder, int speed, uint8_t duration, uint8_t control)
Definition: EVShield.cpp:468
EVShieldBank::motorSetTimeToRun
bool motorSetTimeToRun(SH_Motor which_motor, int seconds)
Definition: EVShield.cpp:271
SH_HardwareI2C
Definition: SHDefines.h:64
EVShieldBank::motorStop
bool motorStop(SH_Motor which_motors, SH_Next_Action next_action)
Definition: EVShield.cpp:702
EVShieldBank::motorGetCommandRegA
uint8_t motorGetCommandRegA(SH_Motor which_motor)
Definition: EVShield.cpp:301
SH_Direction
SH_Direction
Definition: EVShield.h:159
EVShieldBank::motorGetTimeToRun
uint8_t motorGetTimeToRun(SH_Motor which_motor)
Definition: EVShield.cpp:276
EVShieldBank::motorGetCommandRegB
uint8_t motorGetCommandRegB(SH_Motor which_motor)
Definition: EVShield.cpp:289
EVShieldBank::motorSetSpeed
bool motorSetSpeed(SH_Motor which_motor, int speed)
Definition: EVShield.cpp:259
EVShield::bank_a
EVShieldBank bank_a
Definition: EVShield.h:703
EVShieldBank::sensorSetType
bool sensorSetType(uint8_t which_sensor, uint8_t sensor_type)
Definition: EVShield.cpp:716
EVShieldBank::sensorReadRaw
int sensorReadRaw(uint8_t which_sensor)
Definition: EVShield.cpp:742
EVShieldBank::motorGetTasksRunningByte
uint8_t motorGetTasksRunningByte(SH_Motor which_motor)
Definition: EVShield.cpp:322
EVShieldBank::motorWaitUntilTimeDone
uint8_t motorWaitUntilTimeDone(SH_Motor which_motors)
Definition: EVShield.cpp:526
EVShieldBank::motorSetSpeedPID
bool motorSetSpeedPID(uint16_t Kp, uint16_t Ki, uint16_t Kd)
Definition: EVShield.cpp:339
EVShieldBank::motorIsTimeDone
uint8_t motorIsTimeDone(SH_Motor which_motors)
Definition: EVShield.cpp:493
EVShieldBank::nxshieldGetBatteryVoltage
int nxshieldGetBatteryVoltage()
Definition: EVShield.cpp:225
EVShieldBank::EVShieldIssueCommand
uint8_t EVShieldIssueCommand(char command)
Definition: EVShield.cpp:238
EVShieldBank::motorSetCommandRegB
bool motorSetCommandRegB(SH_Motor which_motor, uint8_t value)
Definition: EVShield.cpp:284
SH_Bank_B
#define SH_Bank_B
Definition: EVShield.h:202
EVShield::bank_b
EVShieldBankB bank_b
Definition: EVShield.h:706
SH_Motor_1
Definition: EVShield.h:142
EVShieldBank::motorRunTachometer
uint8_t motorRunTachometer(SH_Motor which_motors, SH_Direction direction, int speed, long tachometer, SH_Move relative, SH_Completion_Wait wait_for_completion, SH_Next_Action next_action)
Definition: EVShield.cpp:632
EVShieldBank::motorSetCommandRegA
bool motorSetCommandRegA(SH_Motor which_motor, uint8_t value)
Definition: EVShield.cpp:296
EVShield::m_protocol
uint8_t m_protocol
Definition: EVShield.h:700
format_bin
bool format_bin(uint8_t i, char *s)
Definition: EVShield.cpp:56
EVShieldBank::motorReset
bool motorReset()
Definition: EVShield.cpp:391
EVShieldBank::motorResetEncoder
bool motorResetEncoder(SH_Motor which_motor)
Definition: EVShield.cpp:403
SH_Protocols
SH_Protocols
Protocols supported by EVShield.
Definition: SHDefines.h:63
EVShieldBank
This class defines methods for the EVShield Bank(s).
Definition: EVShield.h:306
EVShieldBank::motorRunRotations
uint8_t motorRunRotations(SH_Motor which_motors, SH_Direction direction, int speed, long rotations, SH_Completion_Wait wait_for_completion, SH_Next_Action next_action)
Definition: EVShield.cpp:687
SH_Completion_Dont_Wait
Definition: EVShield.h:176
SH_Next_Action
SH_Next_Action
Definition: EVShield.h:150
SH_Completion_Wait
SH_Completion_Wait
Definition: EVShield.h:175
EVShieldBank::motorRunUnlimited
void motorRunUnlimited(SH_Motor which_motors, SH_Direction direction, int speed)
Definition: EVShield.cpp:601
EVShieldBank::motorGetStatusByte
uint8_t motorGetStatusByte(SH_Motor which_motor)
Definition: EVShield.cpp:315
SH_Motor_2
Definition: EVShield.h:143
EVShieldBank::evshieldGetBatteryVoltage
int evshieldGetBatteryVoltage()
Definition: EVShield.cpp:230
SH_Next_Action_BrakeHold
Definition: EVShield.h:153
EVShieldI2C::init
void init(void *shield, SH_BankPort bp)
Definition: EVShieldI2C.cpp:142
EVShieldI2C
This class implements I2C interfaces used by EVShield.
Definition: EVShieldI2C.h:32
EVShieldBank::motorWaitUntilTachoDone
uint8_t motorWaitUntilTachoDone(SH_Motor which_motors)
Definition: EVShield.cpp:570
EVShieldBank::motorSetEncoderPID
bool motorSetEncoderPID(uint16_t Kp, uint16_t Ki, uint16_t Kd)
Definition: EVShield.cpp:330
SH_Next_Action_Brake
Definition: EVShield.h:152
SH_Motor
SH_Motor
Definition: EVShield.h:141
SH_Bank_A
#define SH_Bank_A
Definition: EVShield.h:197
SHDefines.h
SH_Move_Relative
Definition: EVShield.h:169
EVShieldBank::motorSetPassCount
bool motorSetPassCount(uint8_t pass_count)
Definition: EVShield.cpp:375
SH_Move_Absolute
Definition: EVShield.h:168
SH_Completion_Wait_For
Definition: EVShield.h:177
EVShield
This class defines methods to access EVShield features.
Definition: EVShield.h:694
EVShieldBank::motorRunSeconds
uint8_t motorRunSeconds(SH_Motor which_motors, SH_Direction direction, int speed, uint8_t duration, SH_Completion_Wait wait_for_completion, SH_Next_Action next_action)
Definition: EVShield.cpp:612
EVShieldBank::EVShieldBank
EVShieldBank(uint8_t i2c_address=SH_Bank_A)
Definition: EVShield.cpp:203
SH_Direction_Reverse
Definition: EVShield.h:160
EVShieldBank::motorSetTolerance
bool motorSetTolerance(uint8_t tolerance)
Definition: EVShield.cpp:382
SH_Move
SH_Move
Definition: EVShield.h:167
EVShieldBankB
EVShield has two banks. Bank B has few differences from Bank A. This class defines overriding methods...
Definition: EVShield.h:667
EVShieldBank::motorSetEncoderTarget
bool motorSetEncoderTarget(SH_Motor which_motor, long target)
Definition: EVShield.cpp:245
SH_Direction_Forward
Definition: EVShield.h:161
EVShieldBank::motorStartBothInSync
bool motorStartBothInSync()
Definition: EVShield.cpp:397
EVShieldBank::motorIsTachoDone
uint8_t motorIsTachoDone(SH_Motor which_motors)
Definition: EVShield.cpp:538
EVShieldBank::motorGetEncoderTarget
long motorGetEncoderTarget(SH_Motor which_motor)
Definition: EVShield.cpp:251
EVShieldBank::motorGetEncoderPosition
int32_t motorGetEncoderPosition(SH_Motor which_motor)
Definition: EVShield.cpp:308
EVShieldBank::motorRunDegrees
uint8_t motorRunDegrees(SH_Motor which_motors, SH_Direction direction, int speed, long degrees, SH_Completion_Wait wait_for_completion, SH_Next_Action next_action)
Definition: EVShield.cpp:674
EVShieldBank::motorSetSpeedTimeAndControl
bool motorSetSpeedTimeAndControl(SH_Motor which_motors, int speed, uint8_t duration, uint8_t control)
Definition: EVShield.cpp:427
SH_Motor_Both
Definition: EVShield.h:144