MyoSyn
A C++ library control synthetic muscles in tendon-driven robots
Loading...
Searching...
No Matches
myosyn_test.cpp
Go to the documentation of this file.
1// myosyn_test.cpp : This file contains the 'main' function. Program execution begins and ends there.
2//
3
4#include <iostream>
5#include <myosyn.h>
6#include <math.h>
7#include <macrodef.h>
8#include <quickDAQ.h>
9
10using namespace std;
11
12const unsigned nMuscles = 2;
13unsigned muscleID[nMuscles] = { 3,2 };
14
15
16int main()
17{
18 std::cout << "Hello World!\n";
19 unsigned i, j, updateCount;
20 double t;
21 double mySamplingRate = 1000.0; // Hz
22 double samplePeriod = 1 / mySamplingRate;
23 unsigned mySampleCount = 30000;
24 double mc[nMuscles];
25 myosyn *m = new myosyn[nMuscles];
26
27 // Stuff for measuring joint angle sensor
28 double jtAngle;
29
30 //quickDAQinit();
31 //pinMode(5, ANALOG_IN, 31);
32
33 // Initialize myosyn library
35 myosynSamplingRate(mySamplingRate);
36
37 for (i = 0; i < nMuscles; i++) {
38 new(&(m[i])) myosyn(muscleID[i]); // Based on https://stackoverflow.com/a/35089001/4028978
39 }
40 pinMode(5, ANALOG_IN, 31);
41
42 cout << "Muscles initialized. Press any key to calibrate loadcells and wind up motor for each muscle...\n";
43 getchar();
44 for (i = 0; i < nMuscles; i++) {
45 m[i].calibrateTension();
46 m[i].windUp();
47 cout << "\t- Muscle " << i << " wound up and LC calibrated...\n";
48 }
50
51 cout << "Muscle windup complete! Press any key to calibrate encoders for each muscle...\n";
52 getchar();
54 for (i = 0; i < nMuscles; i++) {
55 m[i].calibrateExcursion();
56 cout << "\t- Muscle " << i << " calibrated...\n";
57 }
58
59 cout << "Calibration complete! Let's read data for " << mySampleCount/mySamplingRate << "seconds...\n";
60 getchar();
62 cout << endl;
63 for (i = 0, t = 0.0, updateCount = 0; i < mySampleCount; i++, t += samplePeriod) {
64
65 // Changing motor command every second
66 for (j = 0; j < myosynNumMuscles(); j++) {
67 if (i == 0) {
68 m[j].startMuscleControl();
69 }
70 // write motor command
71 //mc[j] = m[j].getMuscleToneTension() + (updateCount % 2 == 0) ? ((j % 2 == 0) ? (0.1 * (updateCount)) : 0) : ((j % 2 == 0) ? 0 : (0.1 * (updateCount)));
72 mc[j] = max(((j%2==0)?4:4) * cos(2 * 3.1416 * 1 * t + ((j%2==0)?0:3.1416)), 0);
73 //cortexDrive[1] = max((cortexVoluntaryAmp - 0) * sin(2 * 3.1416 * cortexVoluntaryFreq * tick + 3.1416), 0);
74 m[j].setMotorCommand(mc[j]);
75 }
76
77 m[0].executeControl();
79 m[0].readMuscleTension();
81
82 jtAngle = (double) getAnalogInPin(5, 31);
83 printf("%0.3lf: q1 %+06.2lf ", t, jtAngle);
84
85
86 for (j = 0; j < myosynNumMuscles(); j++) {
87 // SCR: DO PRINTING HERE
88 printf("| M(%d): MC %+06.2lfV, MT %+06.2lfN, TE %+06.2lfmm ", \
89 (int)m[j].getChannelID(), \
90 m[j].getMotorCommand(), \
91 m[j].getMuscleTension(), \
92 m[j].getTendonExcursion());
93 }
94 printf("\r");
95 }
96
97 cout << "\nSampling complete! Press any key to wind down motors.\n";
98 getchar();
100 for (i = 0; i < nMuscles; i++) {
101 m[i].windDown();
102 }
103
104 delete[] m;
105 return 0;
106}
107
108// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
109// Debug program: F5 or Debug > Start Debugging menu
110
111// Tips for Getting Started:
112// 1. Use the Solution Explorer window to add/manage files
113// 2. Use the Team Explorer window to connect to source control
114// 3. Use the Output window to see build output and other messages
115// 4. Use the Error List window to view errors
116// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
117// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
This is the class object for each muscle channel.
Definition: myosyn.h:129
void windUp()
Function starts/energizes the motor of the muscle channel to wind up the tendons to prevent slack.
Definition: myosyn.cpp:380
void readMuscleTension()
Uses the DAQ subsystem to read the muscle tension detected by the loadcell and puts it in the interna...
Definition: myosyn.cpp:506
void calibrateTension(double loadCellGain=NAN)
Sets the gain to calibrate the transformation of the loadcell voltage to force in newtons....
Definition: myosyn.cpp:468
void startMuscleControl()
Starts the force controller and update the status of the muscle channel. This function only needs to ...
Definition: myosyn.cpp:589
void executeControl()
Executes the control law set by the controlRoutine function pointer.
Definition: myosyn.cpp:624
void windDown()
Function stops/deenergizes the motor of the muscle channel to wind down the tendons before system shu...
Definition: myosyn.cpp:400
void readTendonExcursion()
Uses the DAQ subsystem to read the tendon excursion detected by the shaft encoder and puts it in the ...
Definition: myosyn.cpp:568
void setMotorCommand(double newCommand)
Sets the current motor command to be sent to the DC motor.
Definition: myosyn.cpp:610
void calibrateExcursion(double spoolDiameter=NAN)
Calibrates the rotary incremental shaft encoder to zero degrees and set the conversion from angle to ...
Definition: myosyn.cpp:536
unsigned myosynNumMuscles()
Returns the number of muscles configured by the library.
Definition: myosyn.cpp:117
@ RING_OF_FIRE
Definition: myosyn.h:24
double myosynSamplingRate(double newSamplingRate=myosyn_samplingRate_global)
Sets the sampling rate of the data acquisition and controller of all the muscle channels....
Definition: myosyn.cpp:136
void myosynWaitForClock()
This blocking function waits for the clock period to finish between two function calls....
Definition: myosyn.cpp:208
void myosynSetConfiguration(DAQarrangement DAQconfiguration)
Set the muscle configuration/arrangement type of the library. List of possibilities in the DAQarrange...
Definition: myosyn.cpp:126
unsigned muscleID[nMuscles]
Definition: myosyn_test.cpp:13
const unsigned nMuscles
Definition: myosyn_test.cpp:12
int main()
Definition: myosyn_test.cpp:16