顯示具有 Q霸小車 標籤的文章。 顯示所有文章
顯示具有 Q霸小車 標籤的文章。 顯示所有文章

2026年1月22日 星期四

[Q霸小車] 第三堂AI鏡頭初體驗



範例一、辦識前進、後退、左轉、右轉、停止


Python:

 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
PlanetX_AILens.init_module()
PlanetX_AILens.switchfunc(PlanetX_AILens.FuncList.CARD)

def on_forever():
    PlanetX_AILens.camera_image()
    if PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.FORWARD):
        basic.show_leds("""
            . . # . .
            . # # # .
            # . # . #
            . . # . .
            . . # . .
            """)
    elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.BACK):
        basic.show_leds("""
            . . # . .
            . . # . .
            # . # . #
            . # # # .
            . . # . .
            """)
    elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.STOP):
        basic.show_leds("""
            . . . . .
            . # # # .
            . # # # .
            . # # # .
            . . . . .
            """)
    elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNLEFT):
        basic.show_leds("""
            . . # . .
            . # . . .
            # # # # #
            . # . . .
            . . # . .
            """)
    elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNRIGHT):
        basic.show_leds("""
            . . # . .
            . . . # .
            # # # # #
            . . . # .
            . . # . .
            """)
basic.forever(on_forever)

範例二、第三堂完整程式

Python程式:


  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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def on_button_pressed_a():
    global mode
    mode = (mode + 1) % 5
    basic.show_number(mode)
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_button_pressed_b():
    global mode
    if mode > 0:
        mode = mode - 1
    else:
        mode = 4
    basic.show_number(mode)
input.on_button_pressed(Button.B, on_button_pressed_b)

mode = 0
PlanetX_AILens.init_module()
PlanetX_AILens.switchfunc(PlanetX_AILens.FuncList.CARD)
mode = 0
basic.show_number(mode)
car = 0

def on_forever():
    global car
    PlanetX_AILens.camera_image()
    if mode == 0:
        if PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.FORWARD):
            basic.show_leds("""
                . . # . .
                . # # # .
                # . # . #
                . . # . .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.BACK):
            basic.show_leds("""
                . . # . .
                . . # . .
                # . # . #
                . # # # .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.STOP):
            basic.show_leds("""
                . . . . .
                . # # # .
                . # # # .
                . # # # .
                . . . . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNLEFT):
            basic.show_leds("""
                . . # . .
                . # . . .
                # # # # #
                . # . . .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNRIGHT):
            basic.show_leds("""
                . . # . .
                . . . # .
                # # # # #
                . . . # .
                . . # . .
                """)
    elif mode == 1:
        if PlanetX_AILens.other_card(PlanetX_AILens.otherCards.MOUSE):
            basic.show_string("Mouse")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.MICROBIT):
            basic.show_string("micro\"bit")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.RULER):
            basic.show_string("Ruler")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.CAT):
            basic.show_string("Cat")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.PEAR):
            basic.show_string("Pear")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.SHIP):
            basic.show_string("Ship")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.APPLE):
            basic.show_string("Apple")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.CAR):
            basic.show_string("Car")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.PEN):
            basic.show_string("Pen")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.DOG):
            basic.show_string("Dog")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.UMBRELLA):
            basic.show_string("Umbrella")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.AIRPLANE):
            basic.show_string("Airplane")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.CLOCK):
            basic.show_string("clock")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.GRAPE):
            basic.show_string("Grape")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.CUP):
            basic.show_string("Cup")
    elif mode == 2:
        if PlanetX_AILens.number_card(PlanetX_AILens.numberCards.ZERO):
            basic.show_number(0)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.ONE):
            basic.show_number(1)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.TWO):
            basic.show_number(2)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.THREE):
            basic.show_number(3)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.FOUR):
            basic.show_number(4)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.FIVE):
            basic.show_number(5)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.SIX):
            basic.show_number(6)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.SEVEN):
            basic.show_number(7)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.EIGHT):
            basic.show_number(8)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.NINE):
            basic.show_number(9)
    elif mode == 3:
        if PlanetX_AILens.letter_card(PlanetX_AILens.letterCards.A):
            basic.show_string("A")
        elif PlanetX_AILens.letter_card(PlanetX_AILens.letterCards.B):
            basic.show_string("B")
        elif PlanetX_AILens.letter_card(PlanetX_AILens.letterCards.C):
            basic.show_string("C")
        elif PlanetX_AILens.letter_card(PlanetX_AILens.letterCards.D):
            basic.show_string("D")
        elif PlanetX_AILens.letter_card(PlanetX_AILens.letterCards.E):
            basic.show_string("E")
    elif mode == 4:
        if PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.FORWARD):
            car = 1
            basic.show_leds("""
                . . # . .
                . # # # .
                # . # . #
                . . # . .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.BACK):
            car = 2
            basic.show_leds("""
                . . # . .
                . . # . .
                # . # . #
                . # # # .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.STOP):
            car = 0
            basic.show_leds("""
                . . . . .
                . # # # .
                . # # # .
                . # # # .
                . . . . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNLEFT):
            car = 3
            basic.show_leds("""
                . . # . .
                . # . . .
                # # # # #
                . # . . .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNRIGHT):
            car = 4
            basic.show_leds("""
                . . # . .
                . . . # .
                # # # # #
                . . . # .
                . . # . .
                """)
    basic.pause(100)
basic.forever(on_forever)

def on_forever2():
    global car
    if car == 0:
        cuteBot.stopcar()
    elif car == 1:
        cuteBot.motors(30, 30)
    elif car == 2:
        cuteBot.motors(-30, -30)
    elif car == 3:
        cuteBot.motors(-30, 30)
        basic.pause(250)
        car = 0
    elif car == 4:
        cuteBot.motors(30, -30)
        basic.pause(250)
        car = 0
basic.forever(on_forever2)

2026年1月15日 星期四

[Q霸小車] 第二堂 :micro:bit遙控Q霸小車

 


Q霸小車程式:



Python:

 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
def on_received_string(receivedString):
    global mode
    if receivedString == "stop":
        mode = 0
    elif receivedString == "forward":
        mode = 1
    elif receivedString == "backward":
        mode = 2
    elif receivedString == "turnleft":
        mode = 3
    elif receivedString == "turnright":
        mode = 4
    elif receivedString == "turnon":
        mode = 5
    elif receivedString == "turnoff":
        mode = 6
radio.on_received_string(on_received_string)

def on_received_value(name, value):
    global mode, xValue, yValue
    mode = 7
    if name == "x":
        xValue = value
    if name == "y":
        yValue = value
radio.on_received_value(on_received_value)

yValue = 0
xValue = 0
mode = 0
ID = 1
radio.set_group(ID)
mode = 0
basic.show_number(ID)
basic.pause(5000)
basic.show_icon(IconNames.TORTOISE)
cuteBot.stopcar()

def on_forever():
    if mode == 0:
        cuteBot.stopcar()
    elif mode == 1:
        cuteBot.forward()
        basic.show_leds("""
            . . # . .
            . # # # .
            # . # . #
            . . # . .
            . . # . .
            """)
    elif mode == 2:
        cuteBot.backforward()
        basic.show_leds("""
            . . # . .
            . . # . .
            # . # . #
            . # # # .
            . . # . .
            """)
    elif mode == 3:
        cuteBot.turnleft()
        basic.show_leds("""
            . . # . .
            . # . . .
            # # # # #
            . # . . .
            . . # . .
            """)
    elif mode == 4:
        cuteBot.turnright()
        basic.show_leds("""
            . . # . .
            . . . # .
            # # # # #
            . . . # .
            . . # . .
            """)
    elif mode == 5:
        cuteBot.color_light(cuteBot.RGBLights.RGB_L, 0xff0000)
        cuteBot.color_light(cuteBot.RGBLights.RGB_R, 0xff0000)
        basic.show_icon(IconNames.HEART)
    elif mode == 6:
        cuteBot.closeheadlights()
        basic.clear_screen()
    else:
        cuteBot.motors(xValue + yValue, xValue - yValue)
        basic.show_icon(IconNames.TORTOISE)
basic.forever(on_forever)


遙控端:

Python程式:

 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
def on_button_pressed_a():
    global mode, select
    if select == 1:
        mode = Temp
        basic.show_icon(IconNames.TORTOISE)
        select = 0
    else:
        if mode == 1:
            radio.send_string("turnleft")
            basic.show_leds("""
                . . # . .
                . # . . .
                # # # # #
                . # . . .
                . . # . .
                """)
        elif mode == 3:
            radio.send_string("turnon")
            basic.show_leds("""
                # # # # #
                # # # # #
                # # # # #
                # # # # #
                # # # # #
                """)
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_logo_pressed():
    global Temp, select
    basic.show_icon(IconNames.ANGRY)
    basic.show_number(mode)
    Temp = mode
    select = 1
input.on_logo_event(TouchButtonEvent.PRESSED, on_logo_pressed)

def on_button_pressed_ab():
    if select == 0 and mode == 1:
        radio.send_string("forward")
        basic.show_leds("""
            . . # . .
            . # # # .
            # . # . #
            . . # . .
            . . # . .
            """)
    basic.pause(500)
input.on_button_pressed(Button.AB, on_button_pressed_ab)

def on_button_pressed_b():
    global Temp
    if select == 1:
        Temp = (Temp + 1) % 4
        basic.show_number(Temp)
    else:
        if mode == 1:
            radio.send_string("turnright")
            basic.show_leds("""
                . . # . .
                . . . # .
                # # # # #
                . . . # .
                . . # . .
                """)
        elif mode == 3:
            radio.send_string("turnoff")
            basic.clear_screen()
input.on_button_pressed(Button.B, on_button_pressed_b)

Temp = 0
select = 0
mode = 0
ID = 1
radio.set_group(ID)
basic.show_number(ID)
radio.send_string("stop")
basic.pause(5000)
basic.show_icon(IconNames.TORTOISE)
mode = 0
select = 0

def on_forever():
    if select == 0:
        if mode == 2:
            radio.send_value("x", input.acceleration(Dimension.X) / 10)
            basic.pause(100)
            radio.send_value("y", input.acceleration(Dimension.Y) / 10)
            basic.pause(100)
            basic.show_icon(IconNames.TORTOISE)
    else:
        radio.send_string("stop")
        basic.pause(500)
basic.forever(on_forever)

[Q霸小車] 第一堂 Q霸小車九種能力

 


nakecode:


Python程式:


  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
def on_received_number(receivedNumber):
    global mode
    basic.show_number(receivedNumber)
    mode = receivedNumber
radio.on_received_number(on_received_number)

def on_button_pressed_a():
    global mode
    if select == 0:
        if mode == 1:
            cuteBot.forward()
        elif mode == 6:
            for index in range(5):
                right.show_color(neopixel.colors(NeoPixelColors.YELLOW))
                basic.pause(500)
                cuteBot.color_light(cuteBot.RGBLights.RGB_L, 0xffff00)
                basic.pause(500)
                right.show_color(neopixel.colors(NeoPixelColors.BLACK))
                cuteBot.closeheadlights()
                basic.pause(500)
    else:
        mode = (mode + 1) % 11
        basic.show_number(mode)
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_logo_pressed():
    global select
    basic.show_icon(IconNames.ANGRY)
    select = 1
    basic.show_number(mode)
input.on_logo_event(TouchButtonEvent.PRESSED, on_logo_pressed)

def on_button_pressed_b():
    global select, init
    if select == 0:
        if mode == 1:
            cuteBot.backforward()
        elif mode == 6:
            for index2 in range(5):
                left.show_color(neopixel.colors(NeoPixelColors.YELLOW))
                basic.pause(500)
                cuteBot.color_light(cuteBot.RGBLights.RGB_R, 0xffff00)
                basic.pause(500)
                right.show_color(neopixel.colors(NeoPixelColors.BLACK))
                cuteBot.closeheadlights()
                basic.pause(500)
    else:
        select = 0
        init = 0
        basic.show_icon(IconNames.YES)
input.on_button_pressed(Button.B, on_button_pressed_b)

sonar = 0
strip: neopixel.Strip = None
right_speed = 0
left_speed = 0
init = 0
left: neopixel.Strip = None
right: neopixel.Strip = None
select = 0
mode = 0
mode = 0
select = 0
speed = 0
id2 = 1
radio.set_group(212)
basic.show_icon(IconNames.HEART)

def on_forever():
    global speed, left_speed, right_speed, strip, left, right, init, sonar
    if select == 0:
        if mode == 0:
            basic.show_number(id2)
        elif mode == 1:
            basic.show_icon(IconNames.HEART)
        elif mode == 2:
            basic.show_icon(IconNames.HEART)
            if speed > 100:
                speed = 0
            cuteBot.motors(speed, speed)
            speed += 1
        elif mode == 3:
            basic.show_icon(IconNames.HOUSE)
            cuteBot.forward()
            basic.pause(500)
            cuteBot.motors(100, 40)
            basic.pause(1000)
            cuteBot.forward()
            basic.pause(500)
            cuteBot.motors(40, 100)
            basic.pause(1000)
        elif mode == 4:
            basic.show_icon(IconNames.HEART)
            left_speed = randint(-100, 100)
            right_speed = randint(-100, 100)
            cuteBot.motors(left_speed, right_speed)
            basic.pause(1000)
        elif mode == 5:
            cuteBot.forward()
            if input.light_level() < 10:
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_L, 255, 255, 255)
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_R, 255, 255, 255)
            else:
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_R, 0, 0, 0)
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_L, 0, 0, 0)
        elif mode == 6:
            strip = neopixel.create(DigitalPin.P15, 2, NeoPixelMode.RGB)
            left = strip.range(0, 1)
            right = strip.range(1, 1)
        elif mode == 7:
            if not (cuteBot.tracking(cuteBot.TrackingState.L_R_LINE)):
                cuteBot.motors(20, 20)
            else:
                cuteBot.motors(-50, -50)
                basic.pause(300)
                cuteBot.motors(0, randint(50, 100))
                basic.pause(100)
        elif mode == 8:
            if cuteBot.tracking(cuteBot.TrackingState.L_UNLINE_R_LINE):
                cuteBot.motors(25, 10)
            elif cuteBot.tracking(cuteBot.TrackingState.L_LINE_R_UNLINE):
                cuteBot.motors(10, 25)
            elif cuteBot.tracking(cuteBot.TrackingState.L_R_LINE):
                cuteBot.motors(25, 25)
        elif mode == 9:
            if init == 0:
                init = 1
            else:
                sonar = cuteBot.ultrasonic(cuteBot.SonarUnit.CENTIMETERS)
                cuteBot.motors(30, 30)
                if sonar > 2 and sonar < 10:
                    cuteBot.motors(0, -25)
                    basic.pause(1000)
                else:
                    cuteBot.motors(30, 30)
        else:
            if init == 0:
                init = 1
            else:
                sonar = cuteBot.ultrasonic(cuteBot.SonarUnit.CENTIMETERS)
                cuteBot.motors(30, 30)
                if sonar > 5 and sonar < 10:
                    cuteBot.motors(0, 0)
                elif sonar < 5:
                    cuteBot.motors(-30, -30)
                else:
                    cuteBot.motors(30, 30)
basic.forever(on_forever)