-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pendulum example, fix missing MEL_API in serial port
- Loading branch information
Showing
4 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <MEL/Daq/Quanser/Q8Usb.hpp> | ||
#include <MEL/Mechatronics.hpp> | ||
#include <MEL/Communications.hpp> | ||
#include <MEL/Core.hpp> | ||
#include <MEL/Math.hpp> | ||
#include <MEL/Devices/Windows/Keyboard.hpp> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters