树梅派MPU6050陀螺仪传感器

 

接线图如下:我只接了SDA和SLA  ,vcc和gnd自己接

 代码:PYTHON

#!/usr/bin/python
import smbus
import math
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)  
GPIO.setup(16, GPIO.OUT)

# Power management registers
power_mgmt_1 = 0x6b
power_mgmt_2 = 0x6c

def read_byte(adr):
    return bus.read_byte_data(address, adr)

def read_word(adr):
    high = bus.read_byte_data(address, adr)
    low = bus.read_byte_data(address, adr+1)
    val = (high << 8) + low
    return val
def read_word_2c(adr):
    val = read_word(adr)
    if (val >= 0x8000):
        return -((65535 - val) + 1)
    else:
        return val

def dist(a,b):
    return math.sqrt((a*a)+(b*b))

def get_y_rotation(x,y,z):
    radians = math.atan2(x, dist(y,z))
    return -math.degrees(radians)

def get_x_rotation(x,y,z):
    radians = math.atan2(y, dist(x,z))
    return math.degrees(radians)
ii=1;
while True:
 print(ii)
 ii=ii+1
 bus = smbus.SMBus(1) # or bus = smbus.SMBus(1) for Revision 2 boards
 address = 0x68       # This is the address value read via the i2cdetect command

 # Now wake the 6050 up as it starts in sleep mode
 bus.write_byte_data(address, power_mgmt_1, 0)


 # print ("-----------------------陀螺仪数据--------------------")
 

 gyro_xout = read_word_2c(0x43)
 gyro_yout = read_word_2c(0x45)
 gyro_zout = read_word_2c(0x47)

# print ("X轴陀螺仪计数值: ", gyro_xout, " X每秒的旋转度数: ", (gyro_xout / 131))
# print ("Y轴陀螺仪计数值: ", gyro_yout, " Y每秒的旋转度数: ", (gyro_yout / 131))
# print ("Z轴陀螺仪计数值: ", gyro_zout, " Z每秒的旋转度数: ", (gyro_zout / 131))


# print ("------加速度数据--------")
 

 accel_xout = read_word_2c(0x3b)
 accel_yout = read_word_2c(0x3d)
 accel_zout = read_word_2c(0x3f)

 accel_xout_scaled = accel_xout / 16384.0
 accel_yout_scaled = accel_yout / 16384.0
 accel_zout_scaled = accel_zout / 16384.0
  
# print ("X轴加速度计数值: ", accel_xout, " X每秒的旋转度数: ", accel_xout_scaled)
# print ("Y轴加速度计数值: ", accel_yout, " Y每秒的旋转度数: ", accel_yout_scaled)
# print ("Z轴加速度计数值: ", accel_zout, " Z每秒的旋转度数: ", accel_zout_scaled)

# print ("X轴旋转度数: " , get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled))

# print ("Y轴旋转度数: " , get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled))
 xx=get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
 yy=get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
 print(xx," ",yy)
 
 
 
 
 
 
 


 
 
 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include "sys.h" #include "delay.h" #include "usart.h" #include "led.h" #include "timer.h" #include "FreeRTOS.h" #include "task.h" #include "debug_cmdshell.h" #include "stabilizer.h" //任务优先级 #define START_TASK_PRIO 1 //任务堆栈大小 #define START_STK_SIZE 128 //任务句柄 TaskHandle_t StartTask_Handler; //任务函数 void start_task(void *pvParameters); //任务优先级 #define TASK2_TASK_PRIO 3 //任务堆栈大小 #define TASK2_STK_SIZE 512 //任务句柄 TaskHandle_t Task2Task_Handler; //任务函数 void task2_task(void *pvParameters); int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4 delay_init(); //延时函数初始化 uart_init(115200); //初始化串口 LED_Init(); //初始化LED stabilizerInit(); //创建开始任务 xTaskCreate((TaskFunction_t )start_task, //任务函数 (const char* )"start_task", //任务名称 (uint16_t )START_STK_SIZE, //任务堆栈大小 (void* )NULL, //传递给任务函数的参数 (UBaseType_t )START_TASK_PRIO, //任务优先级 (TaskHandle_t* )&StartTask_Handler); //任务句柄 vTaskStartScheduler(); //开启任务调度 } //开始任务任务函数 void start_task(void *pvParameters) { taskENTER_CRITICAL(); //进入临界区 xTaskCreate((TaskFunction_t )task2_task, (const char* )"task2_task", (uint16_t )TASK2_STK_SIZE, (void* )NULL, (UBaseType_t )TASK2_TASK_PRIO, (TaskHandle_t* )&Task2Task_Handler); xTaskCreate(stabilizerTask, "STABILIZER", 450, NULL, 5, NULL); /*创建姿态任务*/ vTaskDelete(StartTask_Handler); //删除开始任务 taskEXIT_CRITICAL(); //退出临界区 } //task2任务函数 void task2_task(void *pvParameters) { //u8 task2_num=0; u16 len; while(1) { //task2_num++; //任务2执行次数加1 注意task1_num2加到255的时候会清零!! //printf("任务2已经执行:%d次\r\n",task2_num); if(USART_RX_STA&0x8000) { len=USART_RX_STA&0x3fff; debugcmd_process(USART_

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值