Skip to content

Commit beb35e8

Browse files
Shota AokiTiryoh
andauthored
Add step4, step5 samples (#32)
* Add step4 sample * Add sample step5 * fix step4.c * Fix includes * Remove a trailing newline via strip() * include unistd.h * Change Tab to Space * Remove a trailing newline Co-authored-by: Daisuke Sato <daisuke.sato@rt-net.jp>
1 parent 57f39b0 commit beb35e8

File tree

7 files changed

+187
-0
lines changed

7 files changed

+187
-0
lines changed

SampleProgram/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,15 @@ The combination of LEDs and switches are defined as bellow.
7373
- SW0 : LED3
7474
- SW1 : LED1 & LED2
7575
- SW2 : LED0
76+
77+
# Step4
78+
79+
モータを回して右旋回、左旋回します。
80+
81+
Drive the motors to rotate clockwise and counter-clockwise.
82+
83+
# Step5
84+
85+
ライトセンサの値を読み込みます。
86+
87+
Read the lightsensors values.

SampleProgram/step4.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <fcntl.h>
2+
#include <unistd.h>
3+
#include <stdio.h>
4+
5+
void main(void)
6+
{
7+
int motoren = open("/dev/rtmotoren0",O_WRONLY);
8+
int motor_l = open("/dev/rtmotor_raw_l0",O_WRONLY);
9+
int motor_r = open("/dev/rtmotor_raw_r0",O_WRONLY);
10+
int motor = open("/dev/rtmotor0", O_WRONLY);
11+
12+
printf("Motor On\n");
13+
write(motoren,"1",1);
14+
usleep(500*1000);
15+
16+
printf("Rotate counter-clockwise\n");
17+
write(motor_l,"-400",5);
18+
write(motor_r,"400",5);
19+
usleep(500*1000);
20+
21+
write(motor_l,"0",5);
22+
write(motor_r,"0",5);
23+
usleep(500*1000);
24+
25+
printf("Rotate clockwise\n");
26+
write(motor_l,"400",5);
27+
write(motor_r,"-400",5);
28+
usleep(500*1000);
29+
30+
write(motor_l,"0",5);
31+
write(motor_r,"0",5);
32+
usleep(500*1000);
33+
34+
printf("Rotate clockwise\n");
35+
write(motor,"400 -400 500",15);
36+
usleep(500*1000);
37+
38+
printf("Rotate counter-clockwise\n");
39+
write(motor,"-400 400 500",15);
40+
41+
printf("Motor Off\n");
42+
write(motoren,"0",1);
43+
44+
close(motoren);
45+
close(motor_l);
46+
close(motor_r);
47+
close(motor);
48+
}

SampleProgram/step4.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/python
2+
import time
3+
import sys
4+
5+
filename_motoren = "/dev/rtmotoren0"
6+
filename_motor_r = "/dev/rtmotor_raw_r0"
7+
filename_motor_l = "/dev/rtmotor_raw_l0"
8+
filename_motor = "/dev/rtmotor0"
9+
10+
SPEED = "400"
11+
TIME_MS = "500"
12+
13+
def motor_drive(freq_l="0", freq_r="0"):
14+
with open(filename_motor_l, 'w') as f:
15+
f.write(freq_l)
16+
with open(filename_motor_r, 'w') as f:
17+
f.write(freq_r)
18+
19+
print("Motor On")
20+
with open(filename_motoren, 'w') as f:
21+
f.write("1")
22+
time.sleep(0.5)
23+
24+
print("Rotate counter-clockwise")
25+
motor_drive("-"+SPEED, SPEED)
26+
time.sleep(0.5)
27+
28+
motor_drive("0", "0")
29+
time.sleep(0.5)
30+
31+
print("Rotate clockwise")
32+
motor_drive(SPEED, "-"+SPEED)
33+
time.sleep(0.5)
34+
35+
motor_drive("0", "0")
36+
time.sleep(0.5)
37+
38+
print("Rotate clockwise")
39+
with open(filename_motor, 'w') as f:
40+
f.write(SPEED+" -"+SPEED+" "+TIME_MS)
41+
42+
time.sleep(0.5)
43+
44+
print("Rotate counter-clockwise")
45+
with open(filename_motor, 'w') as f:
46+
f.write("-"+SPEED+" "+SPEED+" "+TIME_MS)
47+
48+
print("Motor Off")
49+
with open(filename_motoren, 'w') as f:
50+
f.write("0")

SampleProgram/step4.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
MOTOR_EN=/dev/rtmotoren0
4+
MOTOR_R=/dev/rtmotor_raw_r0
5+
MOTOR_L=/dev/rtmotor_raw_l0
6+
MOTOR=/dev/rtmotor0
7+
8+
SPEED=400
9+
TIME_MS=500
10+
11+
echo "Motor On"
12+
echo 1 > $MOTOR_EN
13+
sleep 0.5
14+
15+
echo "Rotate counter-clockwise"
16+
echo -$SPEED > $MOTOR_L
17+
echo $SPEED > $MOTOR_R
18+
sleep 0.5
19+
20+
echo 0 > $MOTOR_L
21+
echo 0 > $MOTOR_R
22+
sleep 0.5
23+
24+
echo "Rotate clockwise"
25+
echo $SPEED > $MOTOR_L
26+
echo -$SPEED > $MOTOR_R
27+
sleep 0.5
28+
29+
echo 0 > $MOTOR_L
30+
echo 0 > $MOTOR_R
31+
sleep 0.5
32+
33+
echo "Rotate clockwise"
34+
echo $SPEED -$SPEED $TIME_MS > $MOTOR
35+
sleep 0.5
36+
37+
echo "Rotate counter-clockwise"
38+
echo -$SPEED $SPEED $TIME_MS > $MOTOR
39+
40+
echo "Motor Off"
41+
echo 0 > $MOTOR_EN

SampleProgram/step5.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <fcntl.h>
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
5+
void main(void)
6+
{
7+
int buff_size = 256;
8+
FILE *fp;
9+
10+
char buff[buff_size];
11+
while(1){
12+
if ((fp = fopen("/dev/rtlightsensor0", "r")) != NULL){
13+
while(fgets(buff, buff_size, fp) != NULL){
14+
printf("%s", buff);
15+
}
16+
}
17+
fclose(fp);
18+
usleep(500*1000);
19+
}
20+
}

SampleProgram/step5.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/python
2+
import time
3+
4+
while 1:
5+
with open("/dev/rtlightsensor0", 'r') as f:
6+
print(f.read().strip())
7+
time.sleep(0.5)
8+

SampleProgram/step5.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
while true
4+
do
5+
cat /dev/rtlightsensor0
6+
sleep 0.5
7+
done
8+

0 commit comments

Comments
 (0)