Skip to content

Commit 64dd009

Browse files
committed
mpu wip
1 parent 7dadf70 commit 64dd009

File tree

12 files changed

+163
-56
lines changed

12 files changed

+163
-56
lines changed

coderbot.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,31 @@
2727

2828
# GPIO
2929
# motors
30-
PIN_MOTOR_ENABLE = 22
31-
PIN_LEFT_FORWARD = 25
32-
PIN_LEFT_BACKWARD = 24
33-
PIN_RIGHT_FORWARD = 4
34-
PIN_RIGHT_BACKWARD = 17
30+
PIN_MOTOR_ENABLE = None #22
31+
PIN_LEFT_FORWARD = 17 #25
32+
PIN_LEFT_BACKWARD = 18 # 24
33+
PIN_RIGHT_FORWARD = 22 # 4
34+
PIN_RIGHT_BACKWARD = 23 #17
3535
#?
36-
PIN_PUSHBUTTON = 11
36+
PIN_PUSHBUTTON = 16 #11
3737
# servo
38-
PIN_SERVO_3 = 9
39-
PIN_SERVO_4 = 10
38+
PIN_SERVO_3 = 7 #9
39+
PIN_SERVO_4 = 1 #10
4040
# sonar
41-
PIN_SONAR_1_TRIGGER = 18
42-
PIN_SONAR_1_ECHO = 7
43-
PIN_SONAR_2_TRIGGER = 18
44-
PIN_SONAR_2_ECHO = 8
45-
PIN_SONAR_3_TRIGGER = 18
46-
PIN_SONAR_3_ECHO = 23
41+
PIN_SONAR_1_TRIGGER = 5 #18
42+
PIN_SONAR_1_ECHO = 27 #7
43+
PIN_SONAR_2_TRIGGER = 5 #18
44+
PIN_SONAR_2_ECHO = 6 #8
45+
PIN_SONAR_3_TRIGGER = 5 #18
46+
PIN_SONAR_3_ECHO = 12 #23
47+
PIN_SONAR_4_TRIGGER = 5 #18
48+
PIN_SONAR_4_ECHO = 13 #23
49+
4750
# encoder
4851
PIN_ENCODER_LEFT_A = 14
49-
PIN_ENCODER_LEFT_B = 6
50-
PIN_ENCODER_RIGHT_A = 15
51-
PIN_ENCODER_RIGHT_B = 12
52+
PIN_ENCODER_LEFT_B = 15 #6
53+
PIN_ENCODER_RIGHT_A = 24 #15
54+
PIN_ENCODER_RIGHT_B = 25 #12
5255

5356
# PWM
5457
PWM_FREQUENCY = 100 #Hz
@@ -58,7 +61,7 @@ class CoderBot(object):
5861

5962
# pylint: disable=too-many-instance-attributes
6063

61-
_pin_out = [PIN_MOTOR_ENABLE, PIN_LEFT_FORWARD, PIN_RIGHT_FORWARD, PIN_LEFT_BACKWARD, PIN_RIGHT_BACKWARD, PIN_SERVO_3, PIN_SERVO_4]
64+
_pin_out = [PIN_LEFT_FORWARD, PIN_RIGHT_FORWARD, PIN_LEFT_BACKWARD, PIN_RIGHT_BACKWARD, PIN_SERVO_3, PIN_SERVO_4]
6265

6366
def __init__(self, motor_trim_factor=1.0, encoder=True):
6467
self.pi = pigpio.pi('localhost')
@@ -89,7 +92,8 @@ def __init__(self, motor_trim_factor=1.0, encoder=True):
8992

9093
self.sonar = [sonar.Sonar(self.pi, PIN_SONAR_1_TRIGGER, PIN_SONAR_1_ECHO),
9194
sonar.Sonar(self.pi, PIN_SONAR_2_TRIGGER, PIN_SONAR_2_ECHO),
92-
sonar.Sonar(self.pi, PIN_SONAR_3_TRIGGER, PIN_SONAR_3_ECHO)]
95+
sonar.Sonar(self.pi, PIN_SONAR_3_TRIGGER, PIN_SONAR_3_ECHO),
96+
sonar.Sonar(self.pi, PIN_SONAR_4_TRIGGER, PIN_SONAR_4_ECHO)]
9397

9498
try:
9599
self._ag = mpu.AccelGyro()
@@ -126,11 +130,11 @@ def turn(self, speed=100, elapse=0, steps=-1):
126130
self.motor_control(speed_left=speed_left, speed_right=speed_right, time_elapse=elapse)
127131

128132
def turn_angle(self, speed=100, angle=0):
129-
z = self._ag.get_gyro_data()['z']
133+
z = self._ag.get_gyro()[2]
130134
self.turn(speed, elapse=0)
131-
while abs(z - self._ag.get_gyro_data()['z']) < angle:
135+
while abs(z - self._ag.get_gyro()[2]) < angle:
132136
time.sleep(0.05)
133-
logging.info(self._ag.get_gyro_data()['z'])
137+
logging.info(self._ag.get_gyro()[2])
134138
self.stop()
135139

136140
def forward(self, speed=100, elapse=0, distance=0):
@@ -145,9 +149,22 @@ def left(self, speed=100, elapse=0):
145149
def right(self, speed=100, elapse=0):
146150
self.turn(speed=speed, elapse=elapse)
147151

152+
def servo3(self, angle):
153+
self._servo_control(PIN_SERVO_3, angle)
154+
155+
def servo4(self, angle):
156+
self._servo_control(PIN_SERVO_4, angle)
157+
148158
def get_sonar_distance(self, sonar_id=0):
149159
return self.sonar[sonar_id].get_distance()
150160

161+
def _servo_control(self, pin, angle):
162+
duty = ((angle + 90) * 100 / 180) + 25
163+
164+
self.pi.set_PWM_range(pin, 1000)
165+
self.pi.set_PWM_frequency(pin, 50)
166+
self.pi.set_PWM_dutycycle(pin, duty)
167+
151168
def stop(self):
152169
if self._encoder:
153170
self._twin_motors_enc.stop()

lsm9ds1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def read_values(self):
297297
tempf = (tempc * 9/5) + 32
298298
acc = [c * lsm9ds1.ACC_SENSOR_SCALE for c in acc]
299299
gyro = [g * lsm9ds1.DPS_SENSOR_SCALE for g in gyro]
300-
return tempf, acc, gyro
300+
return tempc, acc, gyro
301301

302302
#
303303
# Raw interface

mpu.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,58 @@
1-
import MPU6050
1+
import lsm9ds1
22
import time
33

44
class AccelGyro:
5-
GYRO_THRESHOLD = 1.5
5+
X_IND = 0
6+
Y_IND = 1
7+
Z_IND = 2
8+
9+
PITCH_IND = 0
10+
ROLL_IND = 1
11+
YAW_IND = 2
612

13+
"""This example shows how to poll the sensor for new data.
14+
It queries the sensor to discover when the accelerometer/gyro
15+
has new data and then reads all the sensors."""
716
def __init__(self):
8-
self.ag = MPU6050.MPU6050(0x68)
9-
self.ag.set_gyro_range(MPU6050.MPU6050.GYRO_RANGE_250DEG)
10-
self.gyro_abs = {'x': 0.0, 'y': 0.0, 'z': 0.0}
11-
self.t = time.time()
12-
13-
def get_gyro_data(self):
14-
gyro_data = self.ag.get_gyro_data()
15-
for k in gyro_data.keys():
16-
if abs(gyro_data[k]) > self.GYRO_THRESHOLD:
17-
self.gyro_abs[k] += gyro_data[k] * (time.time() - self.t)
18-
19-
self.t = time.time()
20-
return self.gyro_abs
17+
self.driver = lsm9ds1.make_i2c(1)
18+
mc = lsm9ds1.MagCalibration(xmin=-0.3612, xmax=-0.17836000000000002,
19+
ymin=-0.08750000000000001, ymax=0.07826000000000001,
20+
heading_offset=95.3491645593403)
21+
self.driver.configure(mc)
22+
23+
24+
GYRO_THRESHOLD = 1.5
25+
26+
def read_ag(self):
27+
temp, acc, gyro = self.driver.read_values()
28+
print("Temp: %.1f °f" % temp)
29+
print("Gyro Roll: %.4f, Pitch: %.4f, Yaw: %.4f" % (gyro[SimpleExample.ROLL_IND],
30+
gyro[SimpleExample.PITCH_IND],
31+
gyro[SimpleExample.YAW_IND]))
32+
print("X: %.4f, Y: %.4f, Z: %.4f" % (acc[SimpleExample.X_IND],
33+
acc[SimpleExample.Y_IND],
34+
acc[SimpleExample.Z_IND]))
35+
36+
def read_magnetometer(self):
37+
hdg = self.driver.mag_heading()
38+
print("Heading: %.2f" % hdg)
39+
40+
41+
def get_gyro(self):
42+
temp, acc, gyro = self.driver.read_values()
43+
return (gyro[SimpleExample.ROLL_IND],
44+
gyro[SimpleExample.PITCH_IND],
45+
gyro[SimpleExample.YAW_IND])
46+
47+
def get_acc(self):
48+
temp, acc, gyro = self.driver.read_values()
49+
return (gyro[SimpleExample.X_IND],
50+
gyro[SimpleExample.Y_IND],
51+
gyro[SimpleExample.Z_IND])
52+
53+
def get_hdg(self):
54+
hdg = self.driver.mag_heading()
55+
56+
def get_temp(self):
57+
temp, acc, gyro = self.driver.read_values()
58+
return temp

mpu2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
import lsm9ds1
44

55
class SimpleExample:
6-
X_IND = 1
7-
Y_IND = 2
8-
Z_IND = 0
6+
X_IND = 0
7+
Y_IND = 1
8+
Z_IND = 2
99

10-
PITCH_IND = 1
11-
ROLL_IND = 0
10+
PITCH_IND = 0
11+
ROLL_IND = 1
1212
YAW_IND = 2
1313

1414
"""This example shows how to poll the sensor for new data.
1515
It queries the sensor to discover when the accelerometer/gyro
1616
has new data and then reads all the sensors."""
1717
def __init__(self):
18-
self.driver = lsm9ds1.make_i2c(0)
18+
self.driver = lsm9ds1.make_i2c(1)
1919
mc = lsm9ds1.MagCalibration(xmin=-0.3612, xmax=-0.17836000000000002,
2020
ymin=-0.08750000000000001, ymax=0.07826000000000001,
2121
heading_offset=95.3491645593403)

rotary_encoder/motorencoder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def control(self, power=100.0, time_elapse=0):
8181
self._direction = 1 if power > 0 else -1 # setting direction according to speed
8282
self._power = power # setting current power
8383

84-
self._pi.write(self._enable_pin, True) # enabling motors
84+
if self._enable_pin is not None:
85+
self._pi.write(self._enable_pin, True) # enabling motors
8586

8687
# going forward
8788
if (self._direction == 1):

rotary_encoder/wheelsaxel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def __init__(self, pi, enable_pin,
3030
# right motor
3131
self._right_motor = MotorEncoder(pi,
3232
enable_pin,
33-
right_forward_pin,
3433
right_backward_pin,
35-
right_encoder_feedback_pin_B,
36-
right_encoder_feedback_pin_A)
34+
right_forward_pin,
35+
right_encoder_feedback_pin_A,
36+
right_encoder_feedback_pin_B)
3737

3838
# other
3939
self._wheelsAxle_lock = threading.Condition() # race condition lock
@@ -86,7 +86,7 @@ def control_time(self, power_left=100, power_right=100, time_elapse=0):
8686
self._is_moving = True
8787

8888
# moving for desired time
89-
sleep(time_elapse)
89+
sleep(max(time_elapse, 0))
9090
self.stop()
9191

9292
""" Motor distance control allows the motors

rotary_encoder_tests/rotary_encoder_coderbot_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
c._twin_motors_enc.control_distance(100, 100, 200)
66

7-
while(True):
8-
print(c._twin_motors_enc.distance())
7+
#while(True):
8+
print(c._twin_motors_enc.distance())

rotary_encoder_tests/rotary_encoder_raw_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
PIN_ENCODER_LEFT_A = 14
2-
PIN_ENCODER_LEFT_B = 6
3-
PIN_ENCODER_RIGHT_A = 15
4-
PIN_ENCODER_RIGHT_B = 12
1+
PIN_ENCODER_LEFT_A = 14
2+
PIN_ENCODER_LEFT_B = 15
3+
PIN_ENCODER_RIGHT_A = 24
4+
PIN_ENCODER_RIGHT_B = 25
55

66
from time import sleep
77
import pigpio

static/js/blockly/blocks.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,38 @@ Blockly.Python['coderbot_adv_motor'] = function(block) {
386386
return code;
387387
};
388388

389+
Blockly.Blocks['coderbot_adv_move_enc'] = {
390+
// Block for moving forward.
391+
init: function() {
392+
this.setHelpUrl('http://code.google.com/p/blockly/wiki/Motor');
393+
this.setColour(40);
394+
395+
this.appendValueInput('SPEED')
396+
.setCheck('Number')
397+
.appendField(Blockly.Msg.CODERBOT_MOVE_ADV_MOTOR + " " + Blockly.Msg.CODERBOT_MOVE_ADV_MOTOR_SPEED);
398+
this.appendValueInput('DISTANCE')
399+
.setCheck('Number')
400+
.appendField(Blockly.Msg.CODERBOT_MOVE_ADV_MOTOR_DISTANCE);
401+
this.setInputsInline(true);
402+
// Assign 'this' to a variable for use in the tooltip closure below.
403+
var thisBlock = this;
404+
this.setTooltip(function() {
405+
var mode = thisBlock.getFieldValue('ACTION');
406+
return TOOLTIPS[mode] + Blockly.Msg.CODERBOT_MOVE_ADV_TIP_TAIL;
407+
});
408+
this.setPreviousStatement(true);
409+
this.setNextStatement(true);
410+
}
411+
};
412+
413+
Blockly.Python['coderbot_adv_move_enc'] = function(block) {
414+
// Generate Python for moving forward.
415+
var speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_NONE);
416+
var distance = Blockly.Python.valueToCode(block, 'DISTANCE', Blockly.Python.ORDER_NONE);
417+
var code = "get_bot().move(speed=" + speed + ", distance=" + distance + ")\n";
418+
return code;
419+
};
420+
389421
Blockly.Blocks['coderbot_adv_stop'] = {
390422
// Block to stop the get_bot().
391423
init: function() {
@@ -1055,7 +1087,8 @@ Blockly.Blocks['coderbot_sonar_get_distance'] = {
10551087
.appendField(Blockly.Msg.CODERBOT_SONAR_GET_DISTANCE)
10561088
.appendField(new Blockly.FieldDropdown([[Blockly.Msg.CODERBOT_SONAR_SENSOR_1, "0"],
10571089
[Blockly.Msg.CODERBOT_SONAR_SENSOR_2, "1"],
1058-
[Blockly.Msg.CODERBOT_SONAR_SENSOR_3, "2"]]), 'SONAR');
1090+
[Blockly.Msg.CODERBOT_SONAR_SENSOR_3, "2"],
1091+
[Blockly.Msg.CODERBOT_SONAR_SENSOR_4, "3"]]), 'SONAR');
10591092
this.setOutput(true, 'Number');
10601093
this.setTooltip(Blockly.Msg.LOGIC_BOOLEAN_TOOLTIP);
10611094
}

static/js/blockly/bot_en.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Blockly.Msg.CODERBOT_MOVE_ADV_MOTOR_SPEED_LEFT = "speed left"
1717
Blockly.Msg.CODERBOT_MOVE_ADV_MOTOR_SPEED_RIGHT = "speed right"
1818
Blockly.Msg.CODERBOT_MOVE_ADV_MOTOR_STEPS_LEFT = "steps left"
1919
Blockly.Msg.CODERBOT_MOVE_ADV_MOTOR_STEPS_RIGHT = "steps right"
20+
Blockly.Msg.CODERBOT_MOVE_ADV_MOTOR_SPEED = "at speed"
21+
Blockly.Msg.CODERBOT_MOVE_ADV_MOTOR_DISTANCE = "distance"
2022
Blockly.Msg.CODERBOT_MOVE_ADV_ELAPSE = "for"
2123
Blockly.Msg.CODERBOT_MOVE_MOTION_DIST = "distance"
2224
Blockly.Msg.CODERBOT_MOVE_MOTION_ANGLE = "angle"
@@ -80,6 +82,7 @@ Blockly.Msg.CODERBOT_SONAR_GET_DISTANCE = "get distance with sonar";
8082
Blockly.Msg.CODERBOT_SONAR_SENSOR_1 = "1";
8183
Blockly.Msg.CODERBOT_SONAR_SENSOR_2 = "2";
8284
Blockly.Msg.CODERBOT_SONAR_SENSOR_3 = "3";
85+
Blockly.Msg.CODERBOT_SONAR_SENSOR_4 = "4";
8386
Blockly.Msg.CODERBOT_EVENT_WHEN = "when";
8487
Blockly.Msg.CODERBOT_EVENT_WITH = "with";
8588
Blockly.Msg.CODERBOT_EVENT_PUBLISH = "publish";

0 commit comments

Comments
 (0)