I would use the following algorithm
-
move 1 front leg until it touches the ground
(if max position reached => error) -
move second front leg until it touches the ground
(if max position reached => error) -
level front legs
-
move one rear leg until it touches the ground
-
move rear front leg until it touches the ground
-
level rear legs
if slope ascending adjust front legs to rear legs ( 2 simultaneously)
else adjust rear legs to front legs simultaneously
(add on)
If you add 4 ping sensors you can see how far the legs have to move approx .
Then you can calculate if it is possible at all before moving the motors. (might save some time)
my 2 cents.