2026年1月17日 星期六

[樹藝AI]時尚兔開車用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
47
48
49
50
51
52
m4Speed = 0
m1Speed = 0
lastError = 0
motorSpeed = 0
error = 0
BASE_SPEED = 60
Kp = 0.05
Kd = 0.06
CutebotPro.pwm_cruise_control(0, 0)
PlanetX_AILens.init_module()
PlanetX_AILens.switchfunc(PlanetX_AILens.FuncList.CARD)
radio.set_group(88)

def on_forever():
    PlanetX_AILens.camera_image()
    if PlanetX_AILens.other_card(PlanetX_AILens.otherCards.GRAPE):
        basic.show_string("G")
        radio.send_string("Grape")
    elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.APPLE):
        basic.show_string("A")
        radio.send_string("Apple")
    elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.PEAR):
        basic.show_string("P")
        radio.send_string("Pear")
    basic.pause(200)
basic.forever(on_forever)

def on_forever2():
    global error, motorSpeed, lastError, m1Speed, m4Speed
    error = CutebotPro.get_offset()
    motorSpeed = Kp * error + Kd * (error - lastError)
    lastError = error
    m1Speed = BASE_SPEED + motorSpeed
    m4Speed = BASE_SPEED - motorSpeed
    if m1Speed < 10:
        m1Speed = 0
    elif m1Speed < 30:
        m1Speed = 10
    elif m1Speed > 40:
        m1Speed = 40
    elif m1Speed > 60:
        m1Speed = 60
    if m4Speed < 10:
        m4Speed = 0
    elif m4Speed < 30:
        m4Speed = 10
    elif m4Speed > 40:
        m4Speed = 40
    elif m4Speed > 60:
        m4Speed = 60
    CutebotPro.pwm_cruise_control(m1Speed, m4Speed)
basic.forever(on_forever2)


沒有留言:

張貼留言