Last active 1 month ago

Revision 693658151bb22b5e28197ca7b564c075b11ffa67

Position.cpp Raw
1///////////////////////////////////////////////////////////////////////////////////////////////
2// SINEIO
3///////////////////////////////////////////////////////////////////////////////////////////////
4
5#include <time.h>
6#include "../myWin826.h"
7#include "../826api.h"
8#include "../RealTime.h"
9
10#define SAMPLE_RATE 1000 // SET ME
11
12// IO card configuration constants
13#define IO_BOARD_NUM 0 // no change
14#define ADC_SLOT 0 // no change
15
16#define DAC_ZERO_OUTPUT 0x8000
17#define DAC_CONFIG_GAIN S826_DAC_SPAN_10_10 // -10 to 10 spans 20 volts so...
18#define DAC_VRANGE 20.0 // This MUST correspond to the DAC_SPAN just above
19#define DAC_CNT_RANGE 0xFFFF // 16-bit DAC
20#define DAC_OFFSET_COUNTS 0x8000 // 32768 offset for a signed desired output mapped to unsigned DAC write.
21
22#define DAC_CHANNEL 7 // SET ME! CHECK YOUR SETUP: DAC channel output
23
24#define ADC_CHANNEL 1 // SET ME! CHECK YOUR SETUP: analog input channel to track
25
26
27#define ADC_CNT_RANGE 0xFFFF // 2^16= 0xFFFF (16 bit converter)
28#define ADC_GAIN S826_ADC_GAIN_1 // -10 to 10 option
29#define ADC_VRANGE 20 // this must correspond to gain setting
30
31
32#define ADC_ENABLE 1
33
34int main()
35{
36 int errcode = S826_ERR_OK;
37 int boardflags = S826_SystemOpen(); // open 826 driver and find all 826 boards
38 int slotdata; // adc data from the slot of interest
39 int ncount = 0;
40 uint dacout;
41
42 double voltagein = 0.0;
43 double voltageout = 0;
44
45 double Kp = 0.008; // Proportional gain [cite: 111]
46 int current_position = 0; // Actual position from encoder
47 int ref_position = 0; // Desired position
48 int error = 0; // Control error
49 char key = '0'; // Keyboard input
50
51 // --- Data Logging Variables ---
52 int data_log[2000]; // Integer array for 2 seconds of data [cite: 111, 112]
53 int log_index = 0;
54
55 // instantiate our "real time" object that will pace our loop
56 RealTime realTime(SAMPLE_RATE);
57
58 // Configure data achqisition interfaces and start them running.
59 X826(S826_AdcSlotConfigWrite(IO_BOARD_NUM, ADC_SLOT, ADC_CHANNEL, 0, ADC_GAIN)); // program adc timeslot attributes: slot, chan, 0us settling time. For -5V-5V use: "S826_ADC_GAIN_2"
60 X826(S826_AdcSlotlistWrite(IO_BOARD_NUM, 1 << ADC_SLOT, S826_BITWRITE)); // enable adc timeslot; disable all other slots
61 X826(S826_AdcEnableWrite(IO_BOARD_NUM, ADC_ENABLE)); // enable adc conversions
62
63 X826(S826_DacRangeWrite(IO_BOARD_NUM, DAC_CHANNEL, DAC_CONFIG_GAIN, 0)); // program dac output range: -10V to +10V
64
65 double Kp = 0.008; // Proportional gain [cite: 111]
66 int current_position = 0; // Actual position from encoder
67 int ref_position = 0; // Desired position
68 int error = 0; // Control error
69 char key = 0; // Keyboard input
70
71 // --- Data Logging Variables ---
72 int data_log[2000]; // Integer array for 2 seconds of data [cite: 111, 112]
73 int log_index = 0;
74
75 uint counts = 0;
76 ref_position = current_position + 500;
77
78 // commence pseudo-real-time loop.
79 realTime.Start();
80
81 while (key != 'q')
82 {
83 // 1. Read Keyboard Input
84 if (_kbhit()) // if you hit a key keyboard then read input [cite: 125]
85 {
86 key = _getch();
87 if (key == 'M') ref_position += 500; // 'right arrow key' increments ref [cite: 128, 132]
88 else if (key == 'K') ref_position -= 500; // 'left arrow key' decrements ref [cite: 129, 133]
89 }
90
91 if (log_index < 2000) {
92 data_log[log_index] = current_position;
93 log_index++;
94 }
95
96 error = ref_position - current_position;
97 voltageout = Kp * error;
98
99 //Saftey
100 if (voltageout > 10.0) voltageout = 10.0;
101 if (voltageout < -10.0) voltageout = -10.0;
102
103 realTime.Sleep();
104 }
105
106 realTime.Stop(log_index); // stop real-time loop
107
108
109 X826(S826_DacDataWrite(IO_BOARD_NUM, DAC_CHANNEL, DAC_ZERO_OUTPUT, 0)); // put to zero at the end
110 S826_SystemClose();
111
112
113 return 0;
114}
115
116
117
118