forked from ddemidov/ev3dev-lang-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ev3dev.h
1846 lines (1440 loc) · 63.8 KB
/
ev3dev.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* C++ API to the sensors, motors, buttons, LEDs and battery of the ev3dev
* Linux kernel for the LEGO Mindstorms EV3 hardware
*
* Copyright (c) 2014 - Franz Detro
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Modification:
* Add new button management for ev3dev Release 02.00.00 (ev3dev-jessie-2014-07-12) - Christophe Chaudelet
*
*/
#pragma once
//-----------------------------------------------------------------------------
//~autogen autogen-header
// Sections of the following code were auto-generated based on spec v1.2.0.
//~autogen
//-----------------------------------------------------------------------------
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <algorithm>
#include <functional>
#include <memory>
//-----------------------------------------------------------------------------
namespace ev3dev {
//-----------------------------------------------------------------------------
typedef std::string device_type;
typedef std::string mode_type;
typedef std::set<mode_type> mode_set;
typedef std::string address_type;
//-----------------------------------------------------------------------------
const address_type INPUT_AUTO; //!< Automatic input selection
const address_type OUTPUT_AUTO; //!< Automatic output selection
#if defined(EV3DEV_PLATFORM_BRICKPI)
constexpr char INPUT_1[] = "ttyAMA0:in1"; //!< Sensor port 1
constexpr char INPUT_2[] = "ttyAMA0:in2"; //!< Sensor port 2
constexpr char INPUT_3[] = "ttyAMA0:in3"; //!< Sensor port 3
constexpr char INPUT_4[] = "ttyAMA0:in4"; //!< Sensor port 4
constexpr char OUTPUT_A[] = "ttyAMA0:outA"; //!< Motor port A
constexpr char OUTPUT_B[] = "ttyAMA0:outB"; //!< Motor port B
constexpr char OUTPUT_C[] = "ttyAMA0:outC"; //!< Motor port C
constexpr char OUTPUT_D[] = "ttyAMA0:outD"; //!< Motor port D
#elif defined(EV3DEV_PLATFORM_BRICKPI3)
constexpr char INPUT_1[] = "spi0.1:S1"; //!< Sensor port 1
constexpr char INPUT_2[] = "spi0.1:S2"; //!< Sensor port 2
constexpr char INPUT_3[] = "spi0.1:S3"; //!< Sensor port 3
constexpr char INPUT_4[] = "spi0.1:S4"; //!< Sensor port 4
constexpr char OUTPUT_A[] = "spi0.1:MA"; //!< Motor port A
constexpr char OUTPUT_B[] = "spi0.1:MB"; //!< Motor port B
constexpr char OUTPUT_C[] = "spi0.1:MC"; //!< Motor port C
constexpr char OUTPUT_D[] = "spi0.1:MD"; //!< Motor port D
#elif defined(EV3DEV_PLATFORM_PISTORMS)
constexpr char INPUT_1[] = "pistorms:BAS1"; //!< Sensor port 1
constexpr char INPUT_2[] = "pistorms:BAS2"; //!< Sensor port 2
constexpr char INPUT_3[] = "pistorms:BBS1"; //!< Sensor port 3
constexpr char INPUT_4[] = "pistorms:BBS2"; //!< Sensor port 4
constexpr char OUTPUT_A[] = "pistorms:BAM1"; //!< Motor port A
constexpr char OUTPUT_B[] = "pistorms:BAM2"; //!< Motor port B
constexpr char OUTPUT_C[] = "pistorms:BBM1"; //!< Motor port C
constexpr char OUTPUT_D[] = "pistorms:BBM2"; //!< Motor port D
#else // assume EV3DEV_PLATFORM_EV3
constexpr char INPUT_1[] = "ev3-ports:in1"; //!< Sensor port 1
constexpr char INPUT_2[] = "ev3-ports:in2"; //!< Sensor port 2
constexpr char INPUT_3[] = "ev3-ports:in3"; //!< Sensor port 3
constexpr char INPUT_4[] = "ev3-ports:in4"; //!< Sensor port 4
constexpr char OUTPUT_A[] = "ev3-ports:outA"; //!< Motor port A
constexpr char OUTPUT_B[] = "ev3-ports:outB"; //!< Motor port B
constexpr char OUTPUT_C[] = "ev3-ports:outC"; //!< Motor port C
constexpr char OUTPUT_D[] = "ev3-ports:outD"; //!< Motor port D
#endif
//-----------------------------------------------------------------------------
// Generic device class.
class device
{
public:
bool connect(const std::string &dir,
const std::string &pattern,
const std::map<std::string, std::set<std::string>> &match) noexcept;
inline bool connected() const { return !_path.empty(); }
int device_index() const;
int get_attr_int (const std::string &name) const;
void set_attr_int (const std::string &name,
int value);
std::string get_attr_string(const std::string &name) const;
void set_attr_string(const std::string &name,
const std::string &value);
std::string get_attr_line (const std::string &name) const;
mode_set get_attr_set (const std::string &name,
std::string *pCur = nullptr) const;
std::string get_attr_from_set(const std::string &name) const;
protected:
std::string _path;
mutable int _device_index = -1;
};
//-----------------------------------------------------------------------------
//~autogen generic-class-description classes.sensor>currentClass
// The sensor class provides a uniform interface for using most of the
// sensors available for the EV3. The various underlying device drivers will
// create a `lego-sensor` device for interacting with the sensors.
//
// Sensors are primarily controlled by setting the `mode` and monitored by
// reading the `value<N>` attributes. Values can be converted to floating point
// if needed by `value<N>` / 10.0 ^ `decimals`.
//
// Since the name of the `sensor<N>` device node does not correspond to the port
// that a sensor is plugged in to, you must look at the `address` attribute if
// you need to know which port a sensor is plugged in to. However, if you don't
// have more than one sensor of each type, you can just look for a matching
// `driver_name`. Then it will not matter which port a sensor is plugged in to - your
// program will still work.
//~autogen
class sensor : protected device
{
public:
typedef device_type sensor_type;
static constexpr char ev3_touch[] = "lego-ev3-touch";
static constexpr char ev3_color[] = "lego-ev3-color";
static constexpr char ev3_ultrasonic[] = "lego-ev3-us";
static constexpr char ev3_gyro[] = "lego-ev3-gyro";
static constexpr char ev3_infrared[] = "lego-ev3-ir";
static constexpr char nxt_touch[] = "lego-nxt-touch";
static constexpr char nxt_light[] = "lego-nxt-light";
static constexpr char nxt_sound[] = "lego-nxt-sound";
static constexpr char nxt_ultrasonic[] = "lego-nxt-us";
static constexpr char nxt_i2c_sensor[] = "nxt-i2c-sensor";
static constexpr char nxt_analog[] = "nxt-analog";
sensor(address_type);
sensor(address_type, const std::set<sensor_type>&);
using device::connected;
using device::device_index;
// Returns the value or values measured by the sensor. Check `num_values` to
// see how many values there are. Values with index >= num_values will return
// an error. The values are fixed point numbers, so check `decimals` to see
// if you need to divide to get the actual value.
int value(unsigned index=0) const;
// The value converted to float using `decimals`.
float float_value(unsigned index=0) const;
// Human-readable name of the connected sensor.
std::string type_name() const;
// Bin Data Format: read-only
// Returns the format of the values in `bin_data` for the current mode.
// Possible values are:
//
// - `u8`: Unsigned 8-bit integer (byte)
// - `s8`: Signed 8-bit integer (sbyte)
// - `u16`: Unsigned 16-bit integer (ushort)
// - `s16`: Signed 16-bit integer (short)
// - `s16_be`: Signed 16-bit integer, big endian
// - `s32`: Signed 32-bit integer (int)
// - `float`: IEEE 754 32-bit floating point (float)
std::string bin_data_format() const { return get_attr_string("bin_data_format"); };
// Bin Data: read-only
// Returns the unscaled raw values in the `value<N>` attributes as raw byte
// array. Use `bin_data_format`, `num_values` and the individual sensor
// documentation to determine how to interpret the data.
const std::vector<char>& bin_data() const;
// Bin Data: read-only
// Writes the unscaled raw values in the `value<N>` attributes into the
// user-provided struct/buffer. Use `bin_data_format`, `num_values` and the
// individual sensor documentation to determine how to interpret the data.
template <class T>
void bin_data(T *buf) const {
bin_data(); // fills _bin_data
std::copy_n(_bin_data.data(), _bin_data.size(), reinterpret_cast<char*>(buf));
}
//~autogen generic-get-set classes.sensor>currentClass
// Address: read-only
// Returns the name of the port that the sensor is connected to, e.g. `ev3:in1`.
// I2C sensors also include the I2C address (decimal), e.g. `ev3:in1:i2c8`.
std::string address() const { return get_attr_string("address"); }
// Command: write-only
// Sends a command to the sensor.
auto set_command(std::string v) -> decltype(*this) {
set_attr_string("command", v);
return *this;
}
// Commands: read-only
// Returns a list of the valid commands for the sensor.
// Returns -EOPNOTSUPP if no commands are supported.
mode_set commands() const { return get_attr_set("commands"); }
// Decimals: read-only
// Returns the number of decimal places for the values in the `value<N>`
// attributes of the current mode.
int decimals() const { return get_attr_int("decimals"); }
// Driver Name: read-only
// Returns the name of the sensor device/driver. See the list of [supported
// sensors] for a complete list of drivers.
std::string driver_name() const { return get_attr_string("driver_name"); }
// Mode: read/write
// Returns the current mode. Writing one of the values returned by `modes`
// sets the sensor to that mode.
std::string mode() const { return get_attr_string("mode"); }
auto set_mode(std::string v) -> decltype(*this) {
set_attr_string("mode", v);
return *this;
}
// Modes: read-only
// Returns a list of the valid modes for the sensor.
mode_set modes() const { return get_attr_set("modes"); }
// Num Values: read-only
// Returns the number of `value<N>` attributes that will return a valid value
// for the current mode.
int num_values() const { return get_attr_int("num_values"); }
// Units: read-only
// Returns the units of the measured value for the current mode. May return
// empty string
std::string units() const { return get_attr_string("units"); }
//~autogen
protected:
sensor() {}
bool connect(const std::map<std::string, std::set<std::string>>&) noexcept;
mutable std::vector<char> _bin_data;
};
//-----------------------------------------------------------------------------
//~autogen generic-class-description classes.i2cSensor>currentClass
// A generic interface to control I2C-type EV3 sensors.
//~autogen
class i2c_sensor : public sensor
{
public:
i2c_sensor(
address_type address = INPUT_AUTO,
const std::set<sensor_type> &types = {}
);
//~autogen generic-get-set classes.i2cSensor>currentClass
// FW Version: read-only
// Returns the firmware version of the sensor if available. Currently only
// I2C/NXT sensors support this.
std::string fw_version() const { return get_attr_string("fw_version"); }
// Poll MS: read/write
// Returns the polling period of the sensor in milliseconds. Writing sets the
// polling period. Setting to 0 disables polling. Minimum value is hard
// coded as 50 msec. Returns -EOPNOTSUPP if changing polling is not supported.
// Currently only I2C/NXT sensors support changing the polling period.
int poll_ms() const { return get_attr_int("poll_ms"); }
auto set_poll_ms(int v) -> decltype(*this) {
set_attr_int("poll_ms", v);
return *this;
}
//~autogen
};
//-----------------------------------------------------------------------------
//~autogen special-sensor-declaration specialSensorTypes.touchSensor>currentClass
// Touch Sensor
class touch_sensor : public sensor
{
public:
touch_sensor(address_type address = INPUT_AUTO);
// Button state
static constexpr char mode_touch[] = "TOUCH";
// A boolean indicating whether the current touch sensor is being
// pressed.
bool is_pressed(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_touch);
return value(0);
}
};
//~autogen
//-----------------------------------------------------------------------------
//~autogen special-sensor-declaration specialSensorTypes.colorSensor>currentClass
// LEGO EV3 color sensor.
class color_sensor : public sensor
{
public:
color_sensor(address_type address = INPUT_AUTO);
// Reflected light. Red LED on.
static constexpr char mode_col_reflect[] = "COL-REFLECT";
// Ambient light. Red LEDs off.
static constexpr char mode_col_ambient[] = "COL-AMBIENT";
// Color. All LEDs rapidly cycling, appears white.
static constexpr char mode_col_color[] = "COL-COLOR";
// Raw reflected. Red LED on
static constexpr char mode_ref_raw[] = "REF-RAW";
// Raw Color Components. All LEDs rapidly cycling, appears white.
static constexpr char mode_rgb_raw[] = "RGB-RAW";
// No color.
static constexpr char color_nocolor[] = "NoColor";
// Black color.
static constexpr char color_black[] = "Black";
// Blue color.
static constexpr char color_blue[] = "Blue";
// Green color.
static constexpr char color_green[] = "Green";
// Yellow color.
static constexpr char color_yellow[] = "Yellow";
// Red color.
static constexpr char color_red[] = "Red";
// White color.
static constexpr char color_white[] = "White";
// Brown color.
static constexpr char color_brown[] = "Brown";
// Reflected light intensity as a percentage. Light on sensor is red.
int reflected_light_intensity(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_col_reflect);
return value(0);
}
// Ambient light intensity. Light on sensor is dimly lit blue.
int ambient_light_intensity(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_col_ambient);
return value(0);
}
// Color detected by the sensor, categorized by overall value.
// - 0: No color
// - 1: Black
// - 2: Blue
// - 3: Green
// - 4: Yellow
// - 5: Red
// - 6: White
// - 7: Brown
int color(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_col_color);
return value(0);
}
// Red, green, and blue components of the detected color, in the range 0-1020.
std::tuple<int, int, int> raw(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_rgb_raw);
return std::make_tuple( value(0), value(1), value(2) );
}
// Red component of the detected color, in the range 0-1020.
int red(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_rgb_raw);
return value(0);
}
// Green component of the detected color, in the range 0-1020.
int green(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_rgb_raw);
return value(1);
}
// Blue component of the detected color, in the range 0-1020.
int blue(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_rgb_raw);
return value(2);
}
};
//~autogen
//-----------------------------------------------------------------------------
//~autogen special-sensor-declaration specialSensorTypes.ultrasonicSensor>currentClass
// LEGO EV3 ultrasonic sensor.
class ultrasonic_sensor : public sensor
{
public:
ultrasonic_sensor(address_type address = INPUT_AUTO);
ultrasonic_sensor(address_type address, const std::set<sensor_type>& sensorTypes);
// Continuous measurement in centimeters.
static constexpr char mode_us_dist_cm[] = "US-DIST-CM";
// Continuous measurement in inches.
static constexpr char mode_us_dist_in[] = "US-DIST-IN";
// Listen.
static constexpr char mode_us_listen[] = "US-LISTEN";
// Single measurement in centimeters.
static constexpr char mode_us_si_cm[] = "US-SI-CM";
// Single measurement in inches.
static constexpr char mode_us_si_in[] = "US-SI-IN";
// Measurement of the distance detected by the sensor,
// in centimeters.
float distance_centimeters(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_us_dist_cm);
return float_value(0);
}
// Measurement of the distance detected by the sensor,
// in inches.
float distance_inches(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_us_dist_in);
return float_value(0);
}
// Value indicating whether another ultrasonic sensor could
// be heard nearby.
bool other_sensor_present(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_us_listen);
return value(0);
}
};
//~autogen
//-----------------------------------------------------------------------------
//~autogen special-sensor-declaration specialSensorTypes.gyroSensor>currentClass
// LEGO EV3 gyro sensor.
class gyro_sensor : public sensor
{
public:
gyro_sensor(address_type address = INPUT_AUTO);
// Angle
static constexpr char mode_gyro_ang[] = "GYRO-ANG";
// Rotational speed
static constexpr char mode_gyro_rate[] = "GYRO-RATE";
// Raw sensor value
static constexpr char mode_gyro_fas[] = "GYRO-FAS";
// Angle and rotational speed
static constexpr char mode_gyro_g_a[] = "GYRO-G&A";
// Calibration ???
static constexpr char mode_gyro_cal[] = "GYRO-CAL";
// The number of degrees that the sensor has been rotated
// since it was put into this mode.
int angle(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_gyro_ang);
return value(0);
}
// The rate at which the sensor is rotating, in degrees/second.
int rate(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_gyro_rate);
return value(0);
}
// Angle (degrees) and Rotational Speed (degrees/second).
std::tuple<int, int> rate_and_angle(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_gyro_g_a);
return std::make_tuple( value(0), value(1) );
}
};
//~autogen
//-----------------------------------------------------------------------------
//~autogen special-sensor-declaration specialSensorTypes.infraredSensor>currentClass
// LEGO EV3 infrared sensor.
class infrared_sensor : public sensor
{
public:
infrared_sensor(address_type address = INPUT_AUTO);
// Proximity
static constexpr char mode_ir_prox[] = "IR-PROX";
// IR Seeker
static constexpr char mode_ir_seek[] = "IR-SEEK";
// IR Remote Control
static constexpr char mode_ir_remote[] = "IR-REMOTE";
// IR Remote Control. State of the buttons is coded in binary
static constexpr char mode_ir_rem_a[] = "IR-REM-A";
// Calibration ???
static constexpr char mode_ir_cal[] = "IR-CAL";
// A measurement of the distance between the sensor and the remote,
// as a percentage. 100% is approximately 70cm/27in.
int proximity(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_ir_prox);
return value(0);
}
};
//~autogen
//-----------------------------------------------------------------------------
//~autogen special-sensor-declaration specialSensorTypes.soundSensor>currentClass
// LEGO NXT Sound Sensor
class sound_sensor : public sensor
{
public:
sound_sensor(address_type address = INPUT_AUTO);
// Sound pressure level. Flat weighting
static constexpr char mode_db[] = "DB";
// Sound pressure level. A weighting
static constexpr char mode_dba[] = "DBA";
// A measurement of the measured sound pressure level, as a
// percent. Uses a flat weighting.
float sound_pressure(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_db);
return float_value(0);
}
// A measurement of the measured sound pressure level, as a
// percent. Uses A-weighting, which focuses on levels up to 55 dB.
float sound_pressure_low(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_dba);
return float_value(0);
}
};
//~autogen
//-----------------------------------------------------------------------------
//~autogen special-sensor-declaration specialSensorTypes.lightSensor>currentClass
// LEGO NXT Light Sensor
class light_sensor : public sensor
{
public:
light_sensor(address_type address = INPUT_AUTO);
// Reflected light. LED on
static constexpr char mode_reflect[] = "REFLECT";
// Ambient light. LED off
static constexpr char mode_ambient[] = "AMBIENT";
// A measurement of the reflected light intensity, as a percentage.
float reflected_light_intensity(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_reflect);
return float_value(0);
}
// A measurement of the ambient light intensity, as a percentage.
float ambient_light_intensity(bool do_set_mode = true) {
if (do_set_mode) set_mode(mode_ambient);
return float_value(0);
}
};
//~autogen
//-----------------------------------------------------------------------------
//~autogen generic-class-description classes.motor>currentClass
// The motor class provides a uniform interface for using motors with
// positional and directional feedback such as the EV3 and NXT motors.
// This feedback allows for precise control of the motors. This is the
// most common type of motor, so we just call it `motor`.
//
// The way to configure a motor is to set the '_sp' attributes when
// calling a command or before. Only in 'run_direct' mode attribute
// changes are processed immediately, in the other modes they only
// take place when a new command is issued.
//~autogen
class motor : protected device
{
public:
typedef device_type motor_type;
motor(address_type);
motor(address_type, const motor_type&);
static constexpr char motor_large[] = "lego-ev3-l-motor";
static constexpr char motor_medium[] = "lego-ev3-m-motor";
using device::connected;
using device::device_index;
//~autogen generic-declare-property-value classes.motor>currentClass
// Run the motor until another command is sent.
static constexpr char command_run_forever[] = "run-forever";
// Run to an absolute position specified by `position_sp` and then
// stop using the action specified in `stop_action`.
static constexpr char command_run_to_abs_pos[] = "run-to-abs-pos";
// Run to a position relative to the current `position` value.
// The new position will be current `position` + `position_sp`.
// When the new position is reached, the motor will stop using
// the action specified by `stop_action`.
static constexpr char command_run_to_rel_pos[] = "run-to-rel-pos";
// Run the motor for the amount of time specified in `time_sp`
// and then stop the motor using the action specified by `stop_action`.
static constexpr char command_run_timed[] = "run-timed";
// Run the motor at the duty cycle specified by `duty_cycle_sp`.
// Unlike other run commands, changing `duty_cycle_sp` while running *will*
// take effect immediately.
static constexpr char command_run_direct[] = "run-direct";
// Stop any of the run commands before they are complete using the
// action specified by `stop_action`.
static constexpr char command_stop[] = "stop";
// Reset all of the motor parameter attributes to their default value.
// This will also have the effect of stopping the motor.
static constexpr char command_reset[] = "reset";
// Sets the normal polarity of the rotary encoder.
static constexpr char encoder_polarity_normal[] = "normal";
// Sets the inversed polarity of the rotary encoder.
static constexpr char encoder_polarity_inversed[] = "inversed";
// With `normal` polarity, a positive duty cycle will
// cause the motor to rotate clockwise.
static constexpr char polarity_normal[] = "normal";
// With `inversed` polarity, a positive duty cycle will
// cause the motor to rotate counter-clockwise.
static constexpr char polarity_inversed[] = "inversed";
// Power is being sent to the motor.
static constexpr char state_running[] = "running";
// The motor is ramping up or down and has not yet reached a constant output level.
static constexpr char state_ramping[] = "ramping";
// The motor is not turning, but rather attempting to hold a fixed position.
static constexpr char state_holding[] = "holding";
// The motor is turning, but cannot reach its `speed_sp`.
static constexpr char state_overloaded[] = "overloaded";
// The motor is not turning when it should be.
static constexpr char state_stalled[] = "stalled";
// Power will be removed from the motor and it will freely coast to a stop.
static constexpr char stop_action_coast[] = "coast";
// Power will be removed from the motor and a passive electrical load will
// be placed on the motor. This is usually done by shorting the motor terminals
// together. This load will absorb the energy from the rotation of the motors and
// cause the motor to stop more quickly than coasting.
static constexpr char stop_action_brake[] = "brake";
// Does not remove power from the motor. Instead it actively try to hold the motor
// at the current position. If an external force tries to turn the motor, the motor
// will `push back` to maintain its position.
static constexpr char stop_action_hold[] = "hold";
//~autogen
//~autogen generic-get-set classes.motor>currentClass
// Address: read-only
// Returns the name of the port that this motor is connected to.
std::string address() const { return get_attr_string("address"); }
// Command: write-only
// Sends a command to the motor controller. See `commands` for a list of
// possible values.
auto set_command(std::string v) -> decltype(*this) {
set_attr_string("command", v);
return *this;
}
// Commands: read-only
// Returns a list of commands that are supported by the motor
// controller. Possible values are `run-forever`, `run-to-abs-pos`, `run-to-rel-pos`,
// `run-timed`, `run-direct`, `stop` and `reset`. Not all commands may be supported.
//
// - `run-forever` will cause the motor to run until another command is sent.
// - `run-to-abs-pos` will run to an absolute position specified by `position_sp`
// and then stop using the action specified in `stop_action`.
// - `run-to-rel-pos` will run to a position relative to the current `position` value.
// The new position will be current `position` + `position_sp`. When the new
// position is reached, the motor will stop using the action specified by `stop_action`.
// - `run-timed` will run the motor for the amount of time specified in `time_sp`
// and then stop the motor using the action specified by `stop_action`.
// - `run-direct` will run the motor at the duty cycle specified by `duty_cycle_sp`.
// Unlike other run commands, changing `duty_cycle_sp` while running *will*
// take effect immediately.
// - `stop` will stop any of the run commands before they are complete using the
// action specified by `stop_action`.
// - `reset` will reset all of the motor parameter attributes to their default value.
// This will also have the effect of stopping the motor.
mode_set commands() const { return get_attr_set("commands"); }
// Count Per Rot: read-only
// Returns the number of tacho counts in one rotation of the motor. Tacho counts
// are used by the position and speed attributes, so you can use this value
// to convert rotations or degrees to tacho counts. (rotation motors only)
int count_per_rot() const { return get_attr_int("count_per_rot"); }
// Count Per M: read-only
// Returns the number of tacho counts in one meter of travel of the motor. Tacho
// counts are used by the position and speed attributes, so you can use this
// value to convert from distance to tacho counts. (linear motors only)
int count_per_m() const { return get_attr_int("count_per_m"); }
// Driver Name: read-only
// Returns the name of the driver that provides this tacho motor device.
std::string driver_name() const { return get_attr_string("driver_name"); }
// Duty Cycle: read-only
// Returns the current duty cycle of the motor. Units are percent. Values
// are -100 to 100.
int duty_cycle() const { return get_attr_int("duty_cycle"); }
// Duty Cycle SP: read/write
// Writing sets the duty cycle setpoint. Reading returns the current value.
// Units are in percent. Valid values are -100 to 100. A negative value causes
// the motor to rotate in reverse.
int duty_cycle_sp() const { return get_attr_int("duty_cycle_sp"); }
auto set_duty_cycle_sp(int v) -> decltype(*this) {
set_attr_int("duty_cycle_sp", v);
return *this;
}
// Full Travel Count: read-only
// Returns the number of tacho counts in the full travel of the motor. When
// combined with the `count_per_m` atribute, you can use this value to
// calculate the maximum travel distance of the motor. (linear motors only)
int full_travel_count() const { return get_attr_int("full_travel_count"); }
// Polarity: read/write
// Sets the polarity of the motor. With `normal` polarity, a positive duty
// cycle will cause the motor to rotate clockwise. With `inversed` polarity,
// a positive duty cycle will cause the motor to rotate counter-clockwise.
// Valid values are `normal` and `inversed`.
std::string polarity() const { return get_attr_string("polarity"); }
auto set_polarity(std::string v) -> decltype(*this) {
set_attr_string("polarity", v);
return *this;
}
// Position: read/write
// Returns the current position of the motor in pulses of the rotary
// encoder. When the motor rotates clockwise, the position will increase.
// Likewise, rotating counter-clockwise causes the position to decrease.
// Writing will set the position to that value.
int position() const { return get_attr_int("position"); }
auto set_position(int v) -> decltype(*this) {
set_attr_int("position", v);
return *this;
}
// Position P: read/write
// The proportional constant for the position PID.
int position_p() const { return get_attr_int("hold_pid/Kp"); }
auto set_position_p(int v) -> decltype(*this) {
set_attr_int("hold_pid/Kp", v);
return *this;
}
// Position I: read/write
// The integral constant for the position PID.
int position_i() const { return get_attr_int("hold_pid/Ki"); }
auto set_position_i(int v) -> decltype(*this) {
set_attr_int("hold_pid/Ki", v);
return *this;
}
// Position D: read/write
// The derivative constant for the position PID.
int position_d() const { return get_attr_int("hold_pid/Kd"); }
auto set_position_d(int v) -> decltype(*this) {
set_attr_int("hold_pid/Kd", v);
return *this;
}
// Position SP: read/write
// Writing specifies the target position for the `run-to-abs-pos` and `run-to-rel-pos`
// commands. Reading returns the current value. Units are in tacho counts. You
// can use the value returned by `counts_per_rot` to convert tacho counts to/from
// rotations or degrees.
int position_sp() const { return get_attr_int("position_sp"); }
auto set_position_sp(int v) -> decltype(*this) {
set_attr_int("position_sp", v);
return *this;
}
// Max Speed: read-only
// Returns the maximum value that is accepted by the `speed_sp` attribute. This
// may be slightly different than the maximum speed that a particular motor can
// reach - it's the maximum theoretical speed.
int max_speed() const { return get_attr_int("max_speed"); }
// Speed: read-only
// Returns the current motor speed in tacho counts per second. Note, this is
// not necessarily degrees (although it is for LEGO motors). Use the `count_per_rot`
// attribute to convert this value to RPM or deg/sec.
int speed() const { return get_attr_int("speed"); }
// Speed SP: read/write
// Writing sets the target speed in tacho counts per second used for all `run-*`
// commands except `run-direct`. Reading returns the current value. A negative
// value causes the motor to rotate in reverse with the exception of `run-to-*-pos`
// commands where the sign is ignored. Use the `count_per_rot` attribute to convert
// RPM or deg/sec to tacho counts per second. Use the `count_per_m` attribute to
// convert m/s to tacho counts per second.
int speed_sp() const { return get_attr_int("speed_sp"); }
auto set_speed_sp(int v) -> decltype(*this) {
set_attr_int("speed_sp", v);
return *this;
}
// Ramp Up SP: read/write
// Writing sets the ramp up setpoint. Reading returns the current value. Units
// are in milliseconds and must be positive. When set to a non-zero value, the
// motor speed will increase from 0 to 100% of `max_speed` over the span of this
// setpoint. The actual ramp time is the ratio of the difference between the
// `speed_sp` and the current `speed` and max_speed multiplied by `ramp_up_sp`.
int ramp_up_sp() const { return get_attr_int("ramp_up_sp"); }
auto set_ramp_up_sp(int v) -> decltype(*this) {
set_attr_int("ramp_up_sp", v);
return *this;
}
// Ramp Down SP: read/write
// Writing sets the ramp down setpoint. Reading returns the current value. Units
// are in milliseconds and must be positive. When set to a non-zero value, the
// motor speed will decrease from 0 to 100% of `max_speed` over the span of this
// setpoint. The actual ramp time is the ratio of the difference between the
// `speed_sp` and the current `speed` and max_speed multiplied by `ramp_down_sp`.
int ramp_down_sp() const { return get_attr_int("ramp_down_sp"); }
auto set_ramp_down_sp(int v) -> decltype(*this) {
set_attr_int("ramp_down_sp", v);
return *this;
}
// Speed P: read/write
// The proportional constant for the speed regulation PID.
int speed_p() const { return get_attr_int("speed_pid/Kp"); }
auto set_speed_p(int v) -> decltype(*this) {
set_attr_int("speed_pid/Kp", v);
return *this;
}
// Speed I: read/write
// The integral constant for the speed regulation PID.
int speed_i() const { return get_attr_int("speed_pid/Ki"); }
auto set_speed_i(int v) -> decltype(*this) {
set_attr_int("speed_pid/Ki", v);
return *this;
}
// Speed D: read/write
// The derivative constant for the speed regulation PID.
int speed_d() const { return get_attr_int("speed_pid/Kd"); }
auto set_speed_d(int v) -> decltype(*this) {
set_attr_int("speed_pid/Kd", v);
return *this;
}
// State: read-only
// Reading returns a list of state flags. Possible flags are
// `running`, `ramping`, `holding`, `overloaded` and `stalled`.
mode_set state() const { return get_attr_set("state"); }
// Stop Action: read/write
// Reading returns the current stop action. Writing sets the stop action.
// The value determines the motors behavior when `command` is set to `stop`.
// Also, it determines the motors behavior when a run command completes. See
// `stop_actions` for a list of possible values.
std::string stop_action() const { return get_attr_string("stop_action"); }
auto set_stop_action(std::string v) -> decltype(*this) {
set_attr_string("stop_action", v);
return *this;
}
// Stop Actions: read-only
// Returns a list of stop actions supported by the motor controller.
// Possible values are `coast`, `brake` and `hold`. `coast` means that power will
// be removed from the motor and it will freely coast to a stop. `brake` means
// that power will be removed from the motor and a passive electrical load will
// be placed on the motor. This is usually done by shorting the motor terminals
// together. This load will absorb the energy from the rotation of the motors and
// cause the motor to stop more quickly than coasting. `hold` does not remove
// power from the motor. Instead it actively tries to hold the motor at the current
// position. If an external force tries to turn the motor, the motor will 'push
// back' to maintain its position.
mode_set stop_actions() const { return get_attr_set("stop_actions"); }
// Time SP: read/write
// Writing specifies the amount of time the motor will run when using the
// `run-timed` command. Reading returns the current value. Units are in
// milliseconds.
int time_sp() const { return get_attr_int("time_sp"); }