From 635c9877abf3d54d67a10da3c35132bba9c4b988 Mon Sep 17 00:00:00 2001 From: epezent Date: Sat, 2 Feb 2019 21:41:02 -0600 Subject: [PATCH] add pendulum example, fix missing MEL_API in serial port --- examples/CMakeLists.txt | 1 + examples/ex_pendulum.cpp | 95 +++++++++++++++++++++++ include/MEL/Communications/SerialPort.hpp | 2 +- include/MEL/Mechatronics.hpp | 1 + 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 examples/ex_pendulum.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5b027ee..67dbb47 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -37,6 +37,7 @@ if(QUANSER) mel_example(haptic_paddle) mel_example(overview) mel_example(ati_sensor) + mel_example(pendulum) endif() if(NI_ARM) diff --git a/examples/ex_pendulum.cpp b/examples/ex_pendulum.cpp new file mode 100644 index 0000000..ab56dd3 --- /dev/null +++ b/examples/ex_pendulum.cpp @@ -0,0 +1,95 @@ +// MIT License +// +// MEL - Mechatronics Engine & Library +// Copyright (c) 2019 Mechatronics and Haptic Interfaces Lab - Rice University +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// Author(s): Evan Pezent (epezent@rice.edu) + +#include +#include +#include +#include +#include +#include + +using namespace mel; + +// create global stop variable CTRL-C handler function +ctrl_bool stop(false); +bool handler(CtrlEvent event) { + if (event == CtrlEvent::CtrlC) + stop = true; + return true; +} + +int main() { + // register handler to gracefully exit program + register_ctrl_handler(handler); + // create Q8 USB DAQ + Q8Usb q8; + q8.open(); + q8.enable(); + // set encoder deg/count + q8.encoder[0].set_units_per_count(2 * PI / 500); + q8.encoder[0].zero(); + // melshare for external scoping + MelShare ms("pendulum"); + // constants + const double sense_gain = 2.20; // A/V + const double commd_gain = 0.5; // A/V + const double kp = 5.0; // A/rad + const double kd = 0.5; // A*s/rad + const double ref = PI; // radians + // PD controlelr (I = 0) + PidController pd(kp, kd, 0); + // control loop timer + Timer timer(hertz(1000)); + // start Q8 watchdog + q8.watchdog.start(); + while (!stop) { + // get current time + Time t = timer.get_elapsed_time_actual(); + // sync Q8 input with real world + q8.update_input(); + // enable/disable pendulum + if (Keyboard::is_key_pressed(Key::Up)) // enable + q8.DO[0].set_value(High); + else if (Keyboard::is_key_pressed(Key::Down)) // disable + q8.DO[0].set_value(Low); + // get current pendulum state + double pos = q8.encoder[0].get_position(); // rad + double vel = q8.encoder[0].get_velocity(); // rad/s + // wrap position to pi so controller behaves + pos = wrap_to_pi(pos); // rad + // measure actual current + double sense_volt = q8.AI[0].get_value(); // V + double sense_curr = sense_volt * sense_gain; // A + // compute PD controller command + double commd_curr = pd.calculate(ref, pos, vel, t); // A + double commd_volt = commd_curr / commd_gain; // V + // set command + q8.AO[0].set_value(commd_volt); + // write data to melshare for scoping + ms.write_data({ pos, vel, sense_curr, commd_curr }); + // sync output w/ real world + q8.update_output(); + // kick watchdog + q8.watchdog.kick(); + // wait for next timestep + timer.wait(); + } + // shutdown Q8 + q8.disable(); + q8.close(); + return 0; +} \ No newline at end of file diff --git a/include/MEL/Communications/SerialPort.hpp b/include/MEL/Communications/SerialPort.hpp index 768a763..db65b94 100644 --- a/include/MEL/Communications/SerialPort.hpp +++ b/include/MEL/Communications/SerialPort.hpp @@ -23,7 +23,7 @@ namespace mel { /// Interface to RS-232 Serial Port -class SerialPort : NonCopyable { +class MEL_API SerialPort : NonCopyable { public: /// Constructor diff --git a/include/MEL/Mechatronics.hpp b/include/MEL/Mechatronics.hpp index dae5666..793e5b4 100644 --- a/include/MEL/Mechatronics.hpp +++ b/include/MEL/Mechatronics.hpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include