summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/itg3200/itg3200.cxx
blob: 1f2f2ffb5279ec4227e078eac790d499b7bfd3b0 (plain)
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
/*
 * Author: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
 * Copyright (c) 2014 Intel Corporation.
 *
 * 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.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <iostream>
#include <string>
#include <stdexcept>
#include <unistd.h>
#include "math.h"
#include "itg3200.h"

#define READ_BUFFER_LENGTH 8

//address and id
#define ITG3200_I2C_ADDR 0x68
#define ITG3200_ID 0x00

//configuration registers
#define ITG3200_SMPLRT_DIV 0x15
#define ITG3200_DLPF_FS 0x16

//interrupt registers
#define ITG3200_INT_CFG 0x17
#define ITG3200_INT_STATUS 0x1A

//data registers (read only)
#define ITG3200_TEMP_H 0x1B
#define ITG3200_TEMP_L 0x1C
#define ITG3200_XOUT_H 0x1D
#define ITG3200_XOUT_L 0x1E
#define ITG3200_YOUT_H 0x1F
#define ITG3200_YOUT_L 0x20
#define ITG3200_ZOUT_H 0x21
#define ITG3200_ZOUT_L 0x22
#define DATA_REG_SIZE 8

//power management
#define ITG3200_PWR_MGM 0x3E

//useful values
#define ITG3200_RESET 0x80
#define ITG3200_SLEEP 0x40
#define ITG3200_WAKEUP 0x00

using namespace upm;

Itg3200::Itg3200(int bus) : m_i2c(bus)
{
    m_i2c.address(ITG3200_I2C_ADDR);
    m_buffer[0] = ITG3200_PWR_MGM;
    m_buffer[1] = ITG3200_RESET;
    m_i2c.write(m_buffer, 2);

    Itg3200::calibrate();
    Itg3200::update();
}

void
Itg3200::calibrate(void)
{
    int reads = 600;
    int delay = 4000; // 4 milliseconds
    int skip = 5; // initial samples to skip
    int temp[3] = {0};

    for(int i = 0; i < reads; i++){

        Itg3200::update();
        if (i >= skip){
            for (int j = 0; j < 3; j++){
                temp[j] += m_rotation[j];
            }
        }
        usleep(delay);
    }

    for(int i = 0; i < 3; i++){
        m_offsets[i] = (-1) * temp[i] / (reads - skip);
    }
}

float
Itg3200::getTemperature()
{
    return 35.0 + (m_temperature + 13200.0) / 280.0;
}

float*
Itg3200::getRotation()
{
    for(int i = 0; i < 3; i++){
        m_angle[i] = m_rotation[i]/14.375;
    }
    return &m_angle[0];
}

int16_t*
Itg3200::getRawValues()
{
    return &m_rotation[0];
}

int16_t
Itg3200::getRawTemp()
{
    return m_temperature;
}

mraa::Result
Itg3200::update(void)
{
    m_i2c.address(ITG3200_I2C_ADDR);
    m_i2c.writeByte(ITG3200_TEMP_H);

    m_i2c.address(ITG3200_I2C_ADDR);
    m_i2c.read(m_buffer, DATA_REG_SIZE);

    //temp
    //
    m_temperature = (m_buffer[0] << 8 ) | m_buffer[1];
    // x
    m_rotation[0] = ((m_buffer[2] << 8 ) | m_buffer[3]) + m_offsets[0];
    // y
    m_rotation[1] = ((m_buffer[4] << 8 ) | m_buffer[5]) + m_offsets[1];
    // z
    m_rotation[2] = ((m_buffer[6] << 8 ) | m_buffer[7]) + m_offsets[2];

    return mraa::SUCCESS;
}