diff --git a/demos/ev3dev-lang-demo.cpp b/demos/ev3dev-lang-demo.cpp index 2323446..01cf286 100644 --- a/demos/ev3dev-lang-demo.cpp +++ b/demos/ev3dev-lang-demo.cpp @@ -178,13 +178,8 @@ void motor_action(motor &dev) << endl << "(i)nfo" << endl << "(c)ommand" << endl - << "st(o)p command [" << dev.stop_command() << "]" << endl - << "speed r(e)gulation [" << dev.speed_regulation_enabled() << "]" << endl; - - if (dev.speed_regulation_enabled()==dev.speed_regulation_on) - cout << "speed (s)etpoint (" << dev.speed_sp() << ")" << endl; - else - cout << "duty cycle (s)etpoint (" << dev.duty_cycle_sp() << ")" << endl; + << "st(o)p command [" << dev.stop_action() << "]" << endl + << "speed (s)etpoint (" << dev.speed_sp() << ")" << endl; cout << "(p)osition setpoint (" << dev.position_sp() << ")" << endl << "ramp (u)p setpoint (" << dev.ramp_up_sp() << ")" << endl @@ -204,12 +199,18 @@ void motor_action(motor &dev) cout << endl; //~autogen generic_report_status classes.motor>currentClass + cout << " Address: "; + try { cout << dev.address() << endl; } + catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Commands: "; try { cout << dev.commands() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Count Per Rot: "; try { cout << dev.count_per_rot() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } + cout << " Count Per M: "; + try { cout << dev.count_per_m() << endl; } + catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Driver Name: "; try { cout << dev.driver_name() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -219,15 +220,12 @@ void motor_action(motor &dev) cout << " Duty Cycle SP: "; try { cout << dev.duty_cycle_sp() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Encoder Polarity: "; - try { cout << dev.encoder_polarity() << endl; } + cout << " Full Travel Count: "; + try { cout << dev.full_travel_count() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Polarity: "; try { cout << dev.polarity() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Address: "; - try { cout << dev.address() << endl; } - catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Position: "; try { cout << dev.position() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -243,6 +241,9 @@ void motor_action(motor &dev) cout << " Position SP: "; try { cout << dev.position_sp() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } + cout << " Max Speed: "; + try { cout << dev.max_speed() << endl; } + catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Speed: "; try { cout << dev.speed() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -255,26 +256,23 @@ void motor_action(motor &dev) cout << " Ramp Down SP: "; try { cout << dev.ramp_down_sp() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Speed Regulation Enabled: "; - try { cout << dev.speed_regulation_enabled() << endl; } - catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Speed Regulation P: "; - try { cout << dev.speed_regulation_p() << endl; } + cout << " Speed P: "; + try { cout << dev.speed_p() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Speed Regulation I: "; - try { cout << dev.speed_regulation_i() << endl; } + cout << " Speed I: "; + try { cout << dev.speed_i() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Speed Regulation D: "; - try { cout << dev.speed_regulation_d() << endl; } + cout << " Speed D: "; + try { cout << dev.speed_d() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " State: "; try { cout << dev.state() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Stop Command: "; - try { cout << dev.stop_command() << endl; } + cout << " Stop Action: "; + try { cout << dev.stop_action() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Stop Commands: "; - try { cout << dev.stop_commands() << endl; } + cout << " Stop Actions: "; + try { cout << dev.stop_actions() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Time SP: "; try { cout << dev.time_sp() << endl; } @@ -289,22 +287,11 @@ void motor_action(motor &dev) cin >> answer; dev.set_command(answer); cout << endl; break; case 'o': - cout << "stop command " << dev.stop_commands() << ": "; - cin >> answer; dev.set_stop_command(answer); cout << endl; - break; - case 'e': - cout << "speed regulation (off, on): "; - cin >> answer; dev.set_speed_regulation_enabled(answer); cout << endl; + cout << "stop command " << dev.stop_actions() << ": "; + cin >> answer; dev.set_stop_action(answer); cout << endl; break; case 's': - if (dev.speed_regulation_enabled()==dev.speed_regulation_on) - { - cout << "speed: "; cin >> new_value; dev.set_speed_sp(new_value); cout << endl; - } - else - { - cout << "duty cycle: "; cin >> new_value; dev.set_duty_cycle_sp(new_value); cout << endl; - } + cout << "speed: "; cin >> new_value; dev.set_speed_sp(new_value); cout << endl; break; case 'p': cout << "position: "; cin >> new_value; dev.set_position_sp(new_value); cout << endl; diff --git a/demos/ev3dev-lang-test.cpp b/demos/ev3dev-lang-test.cpp index bf50af0..31f07c3 100644 --- a/demos/ev3dev-lang-test.cpp +++ b/demos/ev3dev-lang-test.cpp @@ -22,7 +22,7 @@ //----------------------------------------------------------------------------- //~autogen autogen-header -// Sections of the following code were auto-generated based on spec v0.9.3-pre, rev 2. +// Sections of the following code were auto-generated based on spec v1.2.0. //~autogen //----------------------------------------------------------------------------- @@ -55,6 +55,9 @@ void test_sensor(const char *name) << " Current properties are:" << endl; //~autogen generic_report_status classes.sensor>currentClass + cout << " Address: "; + try { cout << dev.address() << endl; } + catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Commands: "; try { cout << dev.commands() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -73,9 +76,6 @@ void test_sensor(const char *name) cout << " Num Values: "; try { cout << dev.num_values() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Address: "; - try { cout << dev.address() << endl; } - catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Units: "; try { cout << dev.units() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -99,12 +99,18 @@ void test_motor(const char *name) << " Current properties are:" << endl; //~autogen generic_report_status classes.motor>currentClass + cout << " Address: "; + try { cout << dev.address() << endl; } + catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Commands: "; try { cout << dev.commands() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Count Per Rot: "; try { cout << dev.count_per_rot() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } + cout << " Count Per M: "; + try { cout << dev.count_per_m() << endl; } + catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Driver Name: "; try { cout << dev.driver_name() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -114,15 +120,12 @@ void test_motor(const char *name) cout << " Duty Cycle SP: "; try { cout << dev.duty_cycle_sp() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Encoder Polarity: "; - try { cout << dev.encoder_polarity() << endl; } + cout << " Full Travel Count: "; + try { cout << dev.full_travel_count() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Polarity: "; try { cout << dev.polarity() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Address: "; - try { cout << dev.address() << endl; } - catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Position: "; try { cout << dev.position() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -138,6 +141,9 @@ void test_motor(const char *name) cout << " Position SP: "; try { cout << dev.position_sp() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } + cout << " Max Speed: "; + try { cout << dev.max_speed() << endl; } + catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Speed: "; try { cout << dev.speed() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -150,26 +156,23 @@ void test_motor(const char *name) cout << " Ramp Down SP: "; try { cout << dev.ramp_down_sp() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Speed Regulation Enabled: "; - try { cout << dev.speed_regulation_enabled() << endl; } + cout << " Speed P: "; + try { cout << dev.speed_p() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Speed Regulation P: "; - try { cout << dev.speed_regulation_p() << endl; } + cout << " Speed I: "; + try { cout << dev.speed_i() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Speed Regulation I: "; - try { cout << dev.speed_regulation_i() << endl; } - catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Speed Regulation D: "; - try { cout << dev.speed_regulation_d() << endl; } + cout << " Speed D: "; + try { cout << dev.speed_d() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " State: "; try { cout << dev.state() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Stop Command: "; - try { cout << dev.stop_command() << endl; } + cout << " Stop Action: "; + try { cout << dev.stop_action() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Stop Commands: "; - try { cout << dev.stop_commands() << endl; } + cout << " Stop Actions: "; + try { cout << dev.stop_actions() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Time SP: "; try { cout << dev.time_sp() << endl; } @@ -193,6 +196,9 @@ void test_dc_motor() << " Current properties are:" << endl; //~autogen generic_report_status classes.dcMotor>currentClass + cout << " Address: "; + try { cout << dev.address() << endl; } + catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Commands: "; try { cout << dev.commands() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -208,9 +214,6 @@ void test_dc_motor() cout << " Polarity: "; try { cout << dev.polarity() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Address: "; - try { cout << dev.address() << endl; } - catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Ramp Down SP: "; try { cout << dev.ramp_down_sp() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -220,8 +223,8 @@ void test_dc_motor() cout << " State: "; try { cout << dev.state() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Stop Commands: "; - try { cout << dev.stop_commands() << endl; } + cout << " Stop Actions: "; + try { cout << dev.stop_actions() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Time SP: "; try { cout << dev.time_sp() << endl; } @@ -245,6 +248,9 @@ void test_servo_motor() << " Current properties are:" << endl; //~autogen generic_report_status classes.servoMotor>currentClass + cout << " Address: "; + try { cout << dev.address() << endl; } + catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Driver Name: "; try { cout << dev.driver_name() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } @@ -260,9 +266,6 @@ void test_servo_motor() cout << " Polarity: "; try { cout << dev.polarity() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } - cout << " Address: "; - try { cout << dev.address() << endl; } - catch(...) { cout << "[" << strerror(errno) << "]" << endl; } cout << " Position SP: "; try { cout << dev.position_sp() << endl; } catch(...) { cout << "[" << strerror(errno) << "]" << endl; } diff --git a/demos/remote-control.cpp b/demos/remote-control.cpp index 1d641a5..3577c37 100644 --- a/demos/remote-control.cpp +++ b/demos/remote-control.cpp @@ -50,7 +50,7 @@ std::function roll(Motor &motor, Leds &leds, int dir) { motor.set_duty_cycle_sp(90 * dir).run_forever(); ev3::led::set_color(leds, dir > 0 ? ev3::led::green : ev3::led::red); } else { - motor.set_stop_command("brake").stop(); + motor.set_stop_action("brake").stop(); for(auto led : leds) led->off(); } }; @@ -81,8 +81,8 @@ int main() { if (ts.is_pressed()) { ev3::sound::speak("Oops, excuse me!"); - lmotor.set_stop_command("brake").stop(); - rmotor.set_stop_command("brake").stop(); + lmotor.set_stop_action("brake").stop(); + rmotor.set_stop_action("brake").stop(); // Turn red lights on ev3::led::set_color(ev3::led::left, ev3::led::red); diff --git a/ev3dev.cpp b/ev3dev.cpp index ed7ea5f..2a43841 100644 --- a/ev3dev.cpp +++ b/ev3dev.cpp @@ -26,7 +26,7 @@ //----------------------------------------------------------------------------- //~autogen autogen-header -// Sections of the following code were auto-generated based on spec v0.9.3-pre, rev 2. +// Sections of the following code were auto-generated based on spec v1.2.0. //~autogen //----------------------------------------------------------------------------- @@ -751,11 +751,9 @@ const std::string motor::encoder_polarity_normal{ "normal" }; const std::string motor::encoder_polarity_inversed{ "inversed" }; const std::string motor::polarity_normal{ "normal" }; const std::string motor::polarity_inversed{ "inversed" }; -const std::string motor::speed_regulation_on{ "on" }; -const std::string motor::speed_regulation_off{ "off" }; -const std::string motor::stop_command_coast{ "coast" }; -const std::string motor::stop_command_brake{ "brake" }; -const std::string motor::stop_command_hold{ "hold" }; +const std::string motor::stop_action_coast{ "coast" }; +const std::string motor::stop_action_brake{ "brake" }; +const std::string motor::stop_action_hold{ "hold" }; //~autogen @@ -821,8 +819,8 @@ const std::string dc_motor::command_run_direct{ "run-direct" }; const std::string dc_motor::command_stop{ "stop" }; const std::string dc_motor::polarity_normal{ "normal" }; const std::string dc_motor::polarity_inversed{ "inversed" }; -const std::string dc_motor::stop_command_coast{ "coast" }; -const std::string dc_motor::stop_command_brake{ "brake" }; +const std::string dc_motor::stop_action_coast{ "coast" }; +const std::string dc_motor::stop_action_brake{ "brake" }; //~autogen @@ -882,12 +880,13 @@ void led::flash(unsigned on_ms, unsigned off_ms) #ifdef EV3DEV_PLATFORM_BRICKPI //~autogen leds-define platforms.brickpi.led>currentClass -led led::blue_led1{"brickpi1:blue:ev3dev"}; -led led::blue_led2{"brickpi2:blue:ev3dev"}; +led led::blue_led1{"brickpi:led1:blue:ev3dev"}; +led led::blue_led2{"brickpi:led2:blue:ev3dev"}; std::vector led::led1{ &led::blue_led1 }; std::vector led::led2{ &led::blue_led2 }; +std::vector led::black{ static_cast(0) }; std::vector led::blue{ static_cast(1) }; //----------------------------------------------------------------------------- @@ -910,11 +909,12 @@ led led::green_right{"ev3:right:green:ev3dev"}; std::vector led::left{ &led::red_left, &led::green_left }; std::vector led::right{ &led::red_right, &led::green_right }; +std::vector led::black{ static_cast(0), static_cast(0) }; std::vector led::red{ static_cast(1), static_cast(0) }; std::vector led::green{ static_cast(0), static_cast(1) }; std::vector led::amber{ static_cast(1), static_cast(1) }; std::vector led::orange{ static_cast(1), static_cast(0.5) }; -std::vector led::yellow{ static_cast(0.5), static_cast(1) }; +std::vector led::yellow{ static_cast(0.1), static_cast(1) }; //----------------------------------------------------------------------------- void led::all_off() { diff --git a/ev3dev.h b/ev3dev.h index ed3ff2c..b660cb8 100644 --- a/ev3dev.h +++ b/ev3dev.h @@ -29,7 +29,7 @@ //----------------------------------------------------------------------------- //~autogen autogen-header -// Sections of the following code were auto-generated based on spec v0.9.3-pre, rev 2. +// Sections of the following code were auto-generated based on spec v1.2.0. //~autogen //----------------------------------------------------------------------------- @@ -199,6 +199,11 @@ class sensor : protected device //~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) { @@ -239,11 +244,6 @@ class sensor : protected device // for the current mode. int num_values() const { return get_attr_int("num_values"); } - // 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"); } - // Units: read-only // Returns the units of the measured value for the current mode. May return // empty string @@ -600,6 +600,11 @@ class light_sensor : public sensor // 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 @@ -622,17 +627,17 @@ class motor : protected device static const std::string command_run_forever; // Run to an absolute position specified by `position_sp` and then - // stop using the command specified in `stop_command`. + // stop using the action specified in `stop_action`. static const std::string command_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 command specified by `stop_command`. + // the action specified by `stop_action`. static const std::string command_run_to_rel_pos; // Run the motor for the amount of time specified in `time_sp` - // and then stop the motor using the command specified by `stop_command`. + // and then stop the motor using the action specified by `stop_action`. static const std::string command_run_timed; // Run the motor at the duty cycle specified by `duty_cycle_sp`. @@ -641,7 +646,7 @@ class motor : protected device static const std::string command_run_direct; // Stop any of the run commands before they are complete using the - // command specified by `stop_command`. + // action specified by `stop_action`. static const std::string command_stop; // Reset all of the motor parameter attributes to their default value. @@ -662,32 +667,29 @@ class motor : protected device // cause the motor to rotate counter-clockwise. static const std::string polarity_inversed; - // The motor controller will vary the power supplied to the motor - // to try to maintain the speed specified in `speed_sp`. - static const std::string speed_regulation_on; - - // The motor controller will use the power specified in `duty_cycle_sp`. - static const std::string speed_regulation_off; - // Power will be removed from the motor and it will freely coast to a stop. - static const std::string stop_command_coast; + static const std::string stop_action_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 const std::string stop_command_brake; + static const std::string stop_action_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 const std::string stop_command_hold; + static const std::string stop_action_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. @@ -703,17 +705,17 @@ class motor : protected device // // - `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 command specified in `stop_command`. + // 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 command specified by `stop_command`. + // 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 command specified by `stop_command`. + // 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 - // command specified by `stop_command`. + // 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"); } @@ -721,10 +723,15 @@ class motor : protected device // 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. In the case of linear - // actuators, the units here will be counts per centimeter. + // 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"); } @@ -737,25 +744,18 @@ class motor : protected device // 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. This value is only used when `speed_regulation` - // is off. + // 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; } - // Encoder Polarity: read/write - // Sets the polarity of the rotary encoder. This is an advanced feature to all - // use of motors that send inversed encoder signals to the EV3. This should - // be set correctly by the driver of a device. It You only need to change this - // value if you are using a unsupported device. Valid values are `normal` and - // `inversed`. - std::string encoder_polarity() const { return get_attr_string("encoder_polarity"); } - auto set_encoder_polarity(std::string v) -> decltype(*this) { - set_attr_string("encoder_polarity", 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 @@ -768,10 +768,6 @@ class motor : protected device return *this; } - // Address: read-only - // Returns the name of the port that this motor is connected to. - std::string address() const { return get_attr_string("address"); } - // 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. @@ -818,16 +814,25 @@ class motor : protected device 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. Not, this is + // 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 when `speed_regulation` - // is on. Reading returns the current value. Use the `count_per_rot` attribute - // to convert RPM or deg/sec to tacho counts per second. + // 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); @@ -836,10 +841,10 @@ class motor : protected device // Ramp Up SP: read/write // Writing sets the ramp up setpoint. Reading returns the current value. Units - // are in milliseconds. When set to a value > 0, the motor will ramp the power - // sent to the motor from 0 to 100% duty cycle over the span of this setpoint - // when starting the motor. If the maximum duty cycle is limited by `duty_cycle_sp` - // or speed regulation, the actual ramp time duration will be less than the setpoint. + // 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); @@ -848,48 +853,36 @@ class motor : protected device // Ramp Down SP: read/write // Writing sets the ramp down setpoint. Reading returns the current value. Units - // are in milliseconds. When set to a value > 0, the motor will ramp the power - // sent to the motor from 100% duty cycle down to 0 over the span of this setpoint - // when stopping the motor. If the starting duty cycle is less than 100%, the - // ramp time duration will be less than the full span of the setpoint. + // 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 Regulation Enabled: read/write - // Turns speed regulation on or off. If speed regulation is on, the motor - // controller will vary the power supplied to the motor to try to maintain the - // speed specified in `speed_sp`. If speed regulation is off, the controller - // will use the power specified in `duty_cycle_sp`. Valid values are `on` and - // `off`. - std::string speed_regulation_enabled() const { return get_attr_string("speed_regulation"); } - auto set_speed_regulation_enabled(std::string v) -> decltype(*this) { - set_attr_string("speed_regulation", v); - return *this; - } - - // Speed Regulation P: read/write + // Speed P: read/write // The proportional constant for the speed regulation PID. - int speed_regulation_p() const { return get_attr_int("speed_pid/Kp"); } - auto set_speed_regulation_p(int v) -> decltype(*this) { + 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 Regulation I: read/write + // Speed I: read/write // The integral constant for the speed regulation PID. - int speed_regulation_i() const { return get_attr_int("speed_pid/Ki"); } - auto set_speed_regulation_i(int v) -> decltype(*this) { + 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 Regulation D: read/write + // Speed D: read/write // The derivative constant for the speed regulation PID. - int speed_regulation_d() const { return get_attr_int("speed_pid/Kd"); } - auto set_speed_regulation_d(int v) -> decltype(*this) { + 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; } @@ -899,29 +892,29 @@ class motor : protected device // `running`, `ramping` `holding` and `stalled`. mode_set state() const { return get_attr_set("state"); } - // Stop Command: read/write - // Reading returns the current stop command. Writing sets the stop command. + // 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_commands` for a list of possible values. - std::string stop_command() const { return get_attr_string("stop_command"); } - auto set_stop_command(std::string v) -> decltype(*this) { - set_attr_string("stop_command", v); + // `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 Commands: read-only - // Returns a list of stop modes supported by the motor controller. + // 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 try to hold the motor at the current + // 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_commands() const { return get_attr_set("stop_commands"); } + 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 @@ -942,17 +935,17 @@ class motor : protected device void run_forever() { set_command("run-forever"); } // Run to an absolute position specified by `position_sp` and then - // stop using the command specified in `stop_command`. + // stop using the action specified in `stop_action`. void run_to_abs_pos() { set_command("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 command specified by `stop_command`. + // the action specified by `stop_action`. void run_to_rel_pos() { set_command("run-to-rel-pos"); } // Run the motor for the amount of time specified in `time_sp` - // and then stop the motor using the command specified by `stop_command`. + // and then stop the motor using the action specified by `stop_action`. void run_timed() { set_command("run-timed"); } // Run the motor at the duty cycle specified by `duty_cycle_sp`. @@ -961,7 +954,7 @@ class motor : protected device void run_direct() { set_command("run-direct"); } // Stop any of the run commands before they are complete using the - // command specified by `stop_command`. + // action specified by `stop_action`. void stop() { set_command("stop"); } // Reset all of the motor parameter attributes to their default value. @@ -1018,7 +1011,7 @@ class dc_motor : protected device static const std::string command_run_forever; // Run the motor for the amount of time specified in `time_sp` - // and then stop the motor using the command specified by `stop_command`. + // and then stop the motor using the action specified by `stop_action`. static const std::string command_run_timed; // Run the motor at the duty cycle specified by `duty_cycle_sp`. @@ -1027,7 +1020,7 @@ class dc_motor : protected device static const std::string command_run_direct; // Stop any of the run commands before they are complete using the - // command specified by `stop_command`. + // action specified by `stop_action`. static const std::string command_stop; // With `normal` polarity, a positive duty cycle will @@ -1039,19 +1032,23 @@ class dc_motor : protected device static const std::string polarity_inversed; // Power will be removed from the motor and it will freely coast to a stop. - static const std::string stop_command_coast; + static const std::string stop_action_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 const std::string stop_command_brake; + static const std::string stop_action_brake; //~autogen //~autogen generic-get-set classes.dcMotor>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 // Sets the command for the motor. Possible values are `run-forever`, `run-timed` and // `stop`. Not all commands may be supported, so be sure to check the contents @@ -1094,10 +1091,6 @@ class dc_motor : protected device return *this; } - // Address: read-only - // Returns the name of the port that this motor is connected to. - std::string address() const { return get_attr_string("address"); } - // Ramp Down SP: read/write // Sets the time in milliseconds that it take the motor to ramp down from 100% // to 0%. Valid values are 0 to 10000 (10 seconds). Default is 0. @@ -1123,18 +1116,18 @@ class dc_motor : protected device // `duty_cycle_sp`. mode_set state() const { return get_attr_set("state"); } - // Stop Command: write-only - // Sets the stop command that will be used when the motor stops. Read - // `stop_commands` to get the list of valid values. - auto set_stop_command(std::string v) -> decltype(*this) { - set_attr_string("stop_command", v); + // Stop Action: write-only + // Sets the stop action that will be used when the motor stops. Read + // `stop_actions` to get the list of valid values. + auto set_stop_action(std::string v) -> decltype(*this) { + set_attr_string("stop_action", v); return *this; } - // Stop Commands: read-only - // Gets a list of stop commands. Valid values are `coast` + // Stop Actions: read-only + // Gets a list of stop actions. Valid values are `coast` // and `brake`. - mode_set stop_commands() const { return get_attr_set("stop_commands"); } + 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 @@ -1155,7 +1148,7 @@ class dc_motor : protected device void run_forever() { set_command("run-forever"); } // Run the motor for the amount of time specified in `time_sp` - // and then stop the motor using the command specified by `stop_command`. + // and then stop the motor using the action specified by `stop_action`. void run_timed() { set_command("run-timed"); } // Run the motor at the duty cycle specified by `duty_cycle_sp`. @@ -1164,7 +1157,7 @@ class dc_motor : protected device void run_direct() { set_command("run-direct"); } // Stop any of the run commands before they are complete using the - // command specified by `stop_command`. + // action specified by `stop_action`. void stop() { set_command("stop"); } @@ -1211,6 +1204,10 @@ class servo_motor : protected device //~autogen generic-get-set classes.servoMotor>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 // Sets the command for the servo. Valid values are `run` and `float`. Setting // to `run` will cause the servo to be driven to the position_sp set in the @@ -1271,10 +1268,6 @@ class servo_motor : protected device return *this; } - // Address: read-only - // Returns the name of the port that this motor is connected to. - std::string address() const { return get_attr_string("address"); } - // Position SP: read/write // Reading returns the current position_sp of the servo. Writing instructs the // servo to move to the specified position_sp. Units are percent. Valid values @@ -1427,6 +1420,7 @@ class led : protected device static std::vector led1; static std::vector led2; + static std::vector black; static std::vector blue; //~autogen @@ -1441,6 +1435,7 @@ class led : protected device static std::vector left; static std::vector right; + static std::vector black; static std::vector red; static std::vector green; static std::vector amber; @@ -1682,6 +1677,11 @@ class lego_port : protected device //~autogen generic-get-set classes.legoPort>currentClass + // Address: read-only + // Returns the name of the port. See individual driver documentation for + // the name that will be returned. + std::string address() const { return get_attr_string("address"); } + // Driver Name: read-only // Returns the name of the driver that loaded this device. You can find the // complete list of drivers in the [list of port drivers]. @@ -1702,11 +1702,6 @@ class lego_port : protected device return *this; } - // Address: read-only - // Returns the name of the port. See individual driver documentation for - // the name that will be returned. - std::string address() const { return get_attr_string("address"); } - // Set Device: write-only // For modes that support it, writing the name of a driver will cause a new // device to be registered for that driver and attached to this port. For diff --git a/tests/api_tests.cpp b/tests/api_tests.cpp index aadbd9e..88be84b 100644 --- a/tests/api_tests.cpp +++ b/tests/api_tests.cpp @@ -17,7 +17,7 @@ void populate_arena(const std::vector &devices) { } TEST_CASE( "Device" ) { - populate_arena({"medium_motor:0", "infrared_sensor:0"}); + populate_arena({"medium_motor:0@outA", "infrared_sensor:0@in1"}); ev3::device d; @@ -56,7 +56,7 @@ TEST_CASE( "Device" ) { } TEST_CASE("Medium Motor") { - populate_arena({"medium_motor:0"}); + populate_arena({"medium_motor:0@outA"}); ev3::medium_motor m; @@ -76,28 +76,26 @@ TEST_CASE("Medium Motor") { std::set state = {"running"}; - REQUIRE(m.count_per_rot() == 360); - REQUIRE(m.commands() == commands); - REQUIRE(m.duty_cycle() == 0); - REQUIRE(m.duty_cycle_sp() == 42); - REQUIRE(m.encoder_polarity() == "normal"); - REQUIRE(m.polarity() == "normal"); - REQUIRE(m.address() == "outA"); - REQUIRE(m.position() == 42); - REQUIRE(m.position_sp() == 42); - REQUIRE(m.ramp_down_sp() == 0); - REQUIRE(m.ramp_up_sp() == 0); - REQUIRE(m.speed() == 0); - REQUIRE(m.speed_regulation_enabled() == "off"); - REQUIRE(m.speed_sp() == 0); - REQUIRE(m.state() == state); - REQUIRE(m.stop_command() == "coast"); - REQUIRE(m.time_sp() == 1000); + REQUIRE(m.count_per_rot() == 360); + REQUIRE(m.commands() == commands); + REQUIRE(m.duty_cycle() == 0); + REQUIRE(m.duty_cycle_sp() == 42); + REQUIRE(m.polarity() == "normal"); + REQUIRE(m.address() == "outA"); + REQUIRE(m.position() == 42); + REQUIRE(m.position_sp() == 42); + REQUIRE(m.ramp_down_sp() == 0); + REQUIRE(m.ramp_up_sp() == 0); + REQUIRE(m.speed() == 0); + REQUIRE(m.speed_sp() == 0); + REQUIRE(m.state() == state); + REQUIRE(m.stop_action() == "coast"); + REQUIRE(m.time_sp() == 1000); } } TEST_CASE("Infrared Sensor") { - populate_arena({"infrared_sensor:0"}); + populate_arena({"infrared_sensor:0@in1"}); ev3::infrared_sensor s; REQUIRE(s.connected());