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)

沒有留言:

張貼留言