aboutsummaryrefslogtreecommitdiff
path: root/apps/systemcmds/calibration/range_cal.c
blob: 159a0d06b7a6caff98c5d3fa0631677a082fdf52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/****************************************************************************
 * range_cal.c
 *
 *   Copyright (C) 2012 Nils Wenzler. All rights reserved.
 *   Authors: Nils Wenzler <wenzlern@ethz.ch>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "calibration.h"

/****************************************************************************
 * Defines
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/
//Store the values here before writing them to global_data_rc_channels
uint16_t max_values[NR_CHANNELS];
uint16_t min_values[NR_CHANNELS];
uint16_t mid_values[NR_CHANNELS];


/****************************************************************************
 * Private Functions
 ****************************************************************************/

/**This sets the middle values
 */
uint8_t set_mid(void)
{
	if (0 == global_data_trylock(&global_data_rc_channels->access_conf)) {
		uint8_t i;

		for (i = 0; i < NR_CHANNELS; i++) {
			if (i == global_data_rc_channels->function[ROLL] ||
			    i == global_data_rc_channels->function[YAW] ||
			    i == global_data_rc_channels->function[PITCH]) {

				mid_values[i] = global_data_rc_channels->chan[i].raw;

			} else {
				mid_values[i] = (max_values[i] + min_values[i]) / 2;
			}

		}

		global_data_unlock(&global_data_rc_channels->access_conf);
		return 0;

	} else
		return -1;
}

/**This gets all current values and writes them to min or max
 */
uint8_t get_value(void)
{
	if (0 == global_data_trylock(&global_data_rc_channels->access_conf)) {
		uint8_t i;

		for (i = 0; i < NR_CHANNELS; i++) {
			//see if the value is bigger or smaller than 1500 (roughly neutral)
			//and write to the appropriate array
			if (global_data_rc_channels->chan[i].raw != 0 &&
			    global_data_rc_channels->chan[i].raw < 1500) {
				min_values[i] = global_data_rc_channels->chan[i].raw;

			} else if (global_data_rc_channels->chan[i].raw != 0 &&
				   global_data_rc_channels->chan[i].raw > 1500) {
				max_values[i] = global_data_rc_channels->chan[i].raw;

			} else {
				max_values[i] = 0;
				min_values[i] = 0;
			}
		}

		global_data_unlock(&global_data_rc_channels->access_conf);
		return 0;

	} else
		return -1;
}


void write_data(void)
{
	//  global_data_lock(&global_data_rc_channels->access_conf);
	//  uint8_t i;
	//  for(i=0; i < NR_CHANNELS; i++){
	//    //Write the data to global_data_rc_channels (if not 0)
	//    if(mid_values[i]!=0 && min_values[i]!=0 && max_values[i]!=0){
	//      global_data_rc_channels->chan[i].scaling_factor =
	//        10000/((max_values[i] - min_values[i])/2);
	//
	//      global_data_rc_channels->chan[i].mid = mid_values[i];
	//    }
	//
	//    printf("Channel %i\t Function %s \t\t Min %i\t\t Max %i\t\t Scaling Factor: %i\t Middle Value %i\n",
	//        i,
	//        global_data_rc_channels->function_name[global_data_rc_channels->function[i]],
	//        min_values[i], max_values[i],
	//        global_data_rc_channels->chan[i].scaling_factor,
	//        global_data_rc_channels->chan[i].mid);
	//  }
	//  global_data_unlock(&global_data_rc_channels->access_conf);

	//Write to the Parameter storage

	global_data_parameter_storage->pm.param_values[PARAM_RC1_MIN] = min_values[0];
	global_data_parameter_storage->pm.param_values[PARAM_RC2_MIN] = min_values[1];
	global_data_parameter_storage->pm.param_values[PARAM_RC3_MIN] = min_values[2];
	global_data_parameter_storage->pm.param_values[PARAM_RC4_MIN] = min_values[3];
	global_data_parameter_storage->pm.param_values[PARAM_RC5_MIN] = min_values[4];
	global_data_parameter_storage->pm.param_values[PARAM_RC6_MIN] = min_values[5];
	global_data_parameter_storage->pm.param_values[PARAM_RC7_MIN] = min_values[6];
	global_data_parameter_storage->pm.param_values[PARAM_RC8_MIN] = min_values[7];


	global_data_parameter_storage->pm.param_values[PARAM_RC1_MAX] = max_values[0];
	global_data_parameter_storage->pm.param_values[PARAM_RC2_MAX] = max_values[1];
	global_data_parameter_storage->pm.param_values[PARAM_RC3_MAX] = max_values[2];
	global_data_parameter_storage->pm.param_values[PARAM_RC4_MAX] = max_values[3];
	global_data_parameter_storage->pm.param_values[PARAM_RC5_MAX] = max_values[4];
	global_data_parameter_storage->pm.param_values[PARAM_RC6_MAX] = max_values[5];
	global_data_parameter_storage->pm.param_values[PARAM_RC7_MAX] = max_values[6];
	global_data_parameter_storage->pm.param_values[PARAM_RC8_MAX] = max_values[7];


	global_data_parameter_storage->pm.param_values[PARAM_RC1_TRIM] = mid_values[0];
	global_data_parameter_storage->pm.param_values[PARAM_RC2_TRIM] = mid_values[1];
	global_data_parameter_storage->pm.param_values[PARAM_RC3_TRIM] = mid_values[2];
	global_data_parameter_storage->pm.param_values[PARAM_RC4_TRIM] = mid_values[3];
	global_data_parameter_storage->pm.param_values[PARAM_RC5_TRIM] = mid_values[4];
	global_data_parameter_storage->pm.param_values[PARAM_RC6_TRIM] = mid_values[5];
	global_data_parameter_storage->pm.param_values[PARAM_RC7_TRIM] = mid_values[6];
	global_data_parameter_storage->pm.param_values[PARAM_RC8_TRIM] = mid_values[7];

	usleep(3e6);
	uint8_t i;

	for (i = 0; i < NR_CHANNELS; i++) {
		printf("Channel %i:\t\t Min %i\t\t Max %i\t\t Scaling Factor: %i\t Middle Value %i\n",
		       i,
		       min_values[i], max_values[i],
		       global_data_rc_channels->chan[i].scaling_factor,
		       global_data_rc_channels->chan[i].mid);
	}


}

/****************************************************************************
 * Public Functions
 ****************************************************************************/
int range_cal(int argc, char *argv[])
{

	printf("The range calibration routine assumes you already did the channel\n");
	printf("assignment\n");
	printf("This routine chooses the minimum, maximum and middle\n");
	printf("value for each channel separatly. The ROLL, PITCH and YAW function\n");
	printf("get their middle value from the RC direct, for the rest it is\n");
	printf("calculated out of the min and max values.\n");
	press_enter();

	printf("Hold both sticks in lower left corner and continue\n ");
	press_enter();
	usleep(500000);

	while (get_value());

	printf("Hold both sticks in upper right corner and continue\n");
	press_enter();
	usleep(500000);

	while (get_value());

	printf("Set the trim to 0 and leave the sticks in the neutral position\n");
	press_enter();

	//Loop until successfull
	while (set_mid());

	//write the values to global_data_rc_channels
	write_data();

	return 0;

}