measure_motors.c 3.57 KB
Newer Older
pgaskell's avatar
pgaskell committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*******************************************************************************
* measure_motor_params.c
*   Template code 
*   Complete this code to automatically measure motor parameters
*   or print out data to be namalyzed in numpy
* 
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <rc/start_stop.h>
#include <rc/encoder_eqep.h>
#include <rc/encoder.h>
#include <rc/adc.h>
#include <rc/time.h>
#include <rc/motor.h>
#include "../common/mb_motor.h"
#include "../common/mb_defs.h"
gxiang's avatar
gxiang committed
23
#include "../common/mb_structs.h"
pgaskell's avatar
pgaskell committed
24
25
26

float enc2meters = (WHEEL_DIAMETER * M_PI) / (GEAR_RATIO * ENCODER_RES);

gxiang's avatar
gxiang committed
27
void test_speed(float duty, float dtime_s,mb_state_t* mb);
pgaskell's avatar
pgaskell committed
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

/*******************************************************************************
* int main() 
*
*******************************************************************************/
int main(){

	// make sure another instance isn't running
    // if return value is -3 then a background process is running with
    // higher privaledges and we couldn't kill it, in which case we should
    // not continue or there may be hardware conflicts. If it returned -4
    // then there was an invalid argument that needs to be fixed.
    if(rc_kill_existing_process(2.0)<-2) return -1;

	// start signal handler so we can exit cleanly
    if(rc_enable_signal_handler()==-1){
                fprintf(stderr,"ERROR: failed to start signal handler\n");
                return -1;
    }

#if defined(MRC_VERSION_1v3) || defined(MRC_VERSION_2v1)
    if(mb_motor_init()<0){
        fprintf(stderr,"ERROR: failed to initialze mb_motors\n");
        return -1;
    }
#endif

#if defined(BEAGLEBONE_BLUE)
    if(rc_motor_init()<0){
        fprintf(stderr,"ERROR: failed to initialze motors\n");
        return -1;
    }
#endif

gxiang's avatar
gxiang committed
62
    if(rc_encoder_init()<0){
pgaskell's avatar
pgaskell committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        fprintf(stderr,"ERROR: failed to initialze encoders\n");
        return -1;
    }
    // make PID file to indicate your project is running
	// due to the check made on the call to rc_kill_existing_process() above
	// we can be fairly confident there is no PID file already and we can
	// make our own safely.
	rc_make_pid_file();

	// done initializing so set state to RUNNING
	rc_set_state(RUNNING);

	if(rc_get_state()==RUNNING){
		rc_nanosleep(1E9); //sleep for 1s
	}
gxiang's avatar
gxiang committed
78
79
80
81
82
    rc_encoder_eqep_write(1,0);
    rc_encoder_eqep_write(2,0);
    mb_state_t mb;
    mb.left_encoder_delta = 0;
    mb.right_encoder_delta = 0;
pgaskell's avatar
pgaskell committed
83

gxiang's avatar
gxiang committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    //set up file for output
    FILE *fpL;
    FILE *fpR;
    fpL = fopen("left_speed.txt", "w+");
    fpR = fopen("right_speed.txt", "w+");
    for (float pwm = 0.0; pwm <=1.0; pwm += 0.05){
       rc_encoder_eqep_write(1,0);
       rc_encoder_eqep_write(2,0);
       test_speed(pwm,1E9,&mb);
       fprintf(fpL,"%f\t%f\n",pwm,mb.left_velocity);
       fprintf(fpR,"%f\t%f\n",pwm,mb.right_velocity);
    }
	// TODO: Plase exit routine here
    fclose(fpL);
    fclose(fpR);
    rc_encoder_cleanup();
    rc_motor_cleanup();
    rc_set_state(EXITING);
pgaskell's avatar
pgaskell committed
102
103
104
105
    // remove pid file LAST
	rc_remove_pid_file();   
	return 0;
}
gxiang's avatar
gxiang committed
106
107
108
109
110
111
112
113
114
115
116
void test_speed(float duty, float dtime_s,mb_state_t* mb){
    rc_motor_set(1,duty);
    rc_motor_set(2,-duty);
    rc_nanosleep(2E8);
    
    rc_nanosleep(dtime_s);
    mb->left_encoder_delta= rc_encoder_eqep_read(1);
    mb->right_encoder_delta= rc_encoder_eqep_read(2);
    mb->left_velocity = mb->left_encoder_delta*1.0 * enc2meters / (dtime_s/1E9);
    mb->right_velocity = mb->right_encoder_delta*1.0 * enc2meters / (dtime_s/1E9);
}